getSupportFragmentManager vs getChildFragmentManager

If you want to have a fragment which behaves as a container of fragments you must use the getChildFragmentManager method of the fragment. If you use the getSupportFragmentManager you will basically use the fragment manager which behaves the way the activity lifecycle goes, not the way your fragment does.

For example I had a fragment which contained a ViewPager – it is called CollectionsFragment. So I had 3 fragments displayed as tabs in it: AllCollectionsFragment, MyCollectionsFragment, FavouriteCollectionsFragment. And I gave the getActivity().getSupportFragmentManager() to the FragmentStatePagerAdapter which I was using.

So this was causing the following behaviour – the onDestroyView/onDestroy/onDetach/onStop methods of the 3 tab fragments not to be called. When I changed to use the getChildFragmentManager everything was OK.

If you want you can check the docs for the two methods:

getChildFragmentManager():
Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

getSupportFragmentManager():
Return the FragmentManager for interacting with fragments associated with this fragment’s activity.

You may also like...