Lv99Zubat
Lv99Zubat

Reputation: 853

getChildFragmentManager() and containerId

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).

Upvotes: 1

Views: 2301

Answers (2)

Lv99Zubat
Lv99Zubat

Reputation: 853

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

getParentFragment().getChildFragmentManager()

not

getChildFragmentManager()

Upvotes: 1

z3n105
z3n105

Reputation: 1995

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)

Upvotes: 1

Related Questions