1

I create a child fragment in the parent fragment code using getChildFragmentManager(), a containerId, and the method .add(). Then I attempt to replace that childFragment inside the onActivityCreated() method of the childfragment code using childFragmentManager(), that same containerId and .replace() but at this point I get an error saying the fragmentTransaction object or whatever can't find a view for my containerId for fragment "nameofchildfragclass".

I verified that the containerId's in the parent when i create the child and inside the child when i try to replace the child are the same (as they should be).

Lv99Zubat
  • 853
  • 2
  • 10
  • 27

2 Answers2

1

When you are in the ChildFragment use getSupportFragmentManager (or getFragmentManager if you are not using support) not getChildFragmentManager.

This will return you the fragment manager that created the ChildFragment which is the Parent of your ChildFragment where you used the ChildFragmentManager.

Each fragment has getFragmentManager and getChildFragmentManager.

Fragment parent: adds Child by using childFragmentManager. (Added Fragment now is Child of this parent Fragment)

Fragment child: Calling getFragmentManager gets the ChildFragmentManager of the parent. Calling getChildFragment will give a new FragmentManager to add child inside this fragment. NOT WHAT YOU NEED)

z3n105
  • 1,935
  • 1
  • 15
  • 14
  • I'm sorry, that second sentence is really confusing to me. I don't understand it. The fragment manager that created the child fragment is getChildFragmentManager(). getSupportFragmentManager() created the parent. – Lv99Zubat Mar 21 '16 at 18:29
  • even though it doesn't make sense to me. I tried using getSupportFragmentManager() to replace the child and I got some strange behavior. The app doesn't crash but the fragment doesnt replace, it just flickers. – Lv99Zubat Mar 21 '16 at 18:31
  • 1
    Yes, the childfragment that you created with childFragmentManager. You said that in that fragment(ChildFragment) you want to switch fragments. To do that you must call getSupportFragmentManager from this childFragment to get the manager that created it. getChildFragmentManager on the childFragment will result a new fragmentmanager which is used to add childs on the childFragment. Hope I made it a bit clearer. I know its a bit confusing. I'll try to edit my answer. – z3n105 Mar 21 '16 at 18:33
  • ahh thanks. "Each fragment has getFragmentManager and getChildFragmentManager." is something I did not know. But doesn't that mean I would need to access the child fragment manager of the parent fragment somehow from the child fragment? I would think getsupportfragmentManager() would replace the child fragment with an overlapping normal fragment which would lead to strange behavior. – Lv99Zubat Mar 21 '16 at 18:46
  • "Calling getFragmentManager gets the ChildFragmentManager of the parent" so sounds like the answer is, yes. and thats how you do it. – Lv99Zubat Mar 21 '16 at 18:48
  • 1
    Yes by getting supportFragmentManager you get the manager that created the fragment. And that is the only manager that keeps track of this fragment. If you want to replace the ChildFragment with a fragment above always use getSupportFragmentManager. Will not lead in strange behavior this is the correc tthing to do. – z3n105 Mar 21 '16 at 18:49
  • 1
    Beware thought if you are in the parent Fragment you use childfragmentManager. In childFragment you use fragmentManager. Only use childFragmentManager for children. – z3n105 Mar 21 '16 at 18:51
  • right, i understand. But like I said, in the child, I tried getSupportFragmentManager to replace and the fragment doesn't replace; it just flickers and keeps the original child. If I use getFragmentManager instead of getSupportFragmentManager, then it works but I still get some strange behavior that I address in another issue [here](http://stackoverflow.com/questions/36091727/what-exactly-happens-when-android-recreates-my-app-on-an-orientation-change-usin). Which is what I'm actually trying to solve. – Lv99Zubat Mar 21 '16 at 19:09
  • 1
    Support Fragments are the ones provided by support v4 library. Either you use ONLY support Fragments or not. I recommend you always use supportFragments to be compatible with previous SDK releases. In a SupportFragment you can MUST use getFragmentManager because since its a support Fragment it gives you directly a supportFragmentManager. It's quite confusing if you just starting to use fragments – z3n105 Mar 21 '16 at 19:12
  • May I ask which resource you used to figure this stuff out? The android docs are very thin when it comes to this stuff. Still not sure why the fragment flickers on a replace, gonna be my next mission. – Lv99Zubat Mar 21 '16 at 19:25
  • 1
    Mostly from experience and self understanding from Android docs. I don't know maybe check this https://guides.codepath.com/android/Creating-and-Using-Fragments. Where nested fragments section. Maybe a book cover this more thoroughly. – z3n105 Mar 21 '16 at 19:31
  • Hey so I found that, when in the child fragment code, if I did getParentFragment().getChildFragment() then I got the behavior I was looking for for. That seems to correctly retrieve the childFragmentManager of the parent. I don't have a source but based off my experience and rational thinking and positive results, I'm inclined to believe there is one getSupportFragmentManager() to each Activity to manage normal fragments and there is one getChildFragmentManager() to each fragment to manage nested fragments. – Lv99Zubat Mar 23 '16 at 17:16
  • "Calling getFragmentManager gets the ChildFragmentManager of the parent. " I think is false. – Lv99Zubat Mar 23 '16 at 17:18
  • I'm not sure Rai if you have a repo I can take a look at your code to see if it's correctly done. It's interesting nontheless I would like to clarify this as well. – z3n105 Mar 23 '16 at 17:25
1

When inside the child code, to replace the child, needed to use

getParentFragment().getChildFragmentManager()

not

getChildFragmentManager()
Lv99Zubat
  • 853
  • 2
  • 10
  • 27