What Is Difference Between Getsupportfragmentmanager() and Getchildfragmentmanager()

getSupportFragmentManager VS getChildFragmentManager

The definition of getChildFragmentManager() is:

Return a private FragmentManager for placing and managing Fragments inside of a Fragment.

Meanwhile the definition of getFragmentManager() is:

Return the FragmentManager for interacting with fragments associated with the fragment's activity.

Basically, the difference is that Fragment's now have their own internal FragmentManager that can handle Fragments. The child FragmentManager is the one that handles Fragments contained within only the Fragment that it was added to. The other FragmentManager is contained within the entire Activity.

To display Fragment1 on MainActivity we must use getSupportFragmentManager()

getSupportFragmentManager().beginTransaction().replace(R.id.container_view_on_main, Fragment1.newInstance());

To display Fragment2 from Fragment1 we have 2 way

Use getFragmentManager()

getFragmentManager().beginTransaction().replace(R.id.container_view_on_main, Fragment1.newInstance());

Use getChildFragmentManager()

First, we have to create a layout with id container_view_on_fragment1 inside fragment1.xml, then

getChildFragmentManager().beginTransaction().replace(R.id.container_view_on_fragment1, Fragment2.newInstance()).commit();

What is difference between getSupportFragmentManager() and getChildFragmentManager()?

The definition of getChildFragmentManager() is:

Return a private FragmentManager for placing and managing Fragments
inside of this Fragment.

Meanwhile the definition of getFragmentManager() (or in this case getSupportFragmentManager()) is:

Return the FragmentManager for interacting with fragments associated
with this fragment's activity.

Basically, the difference is that Fragment's now have their own internal FragmentManager that can handle Fragments. The child FragmentManager is the one that handles Fragments contained within only the Fragment that it was added to. The other FragmentManager is contained within the entire Activity.

In this case, what I'm guessing is you've added the Fragments to the Activity's FragmentManager. You get the child FragmentManager which doesn't contain what you are looking for. Thus you get the exception because it can't find the Fragment with the given ID because it's in a different FragmentManager.

The method getChildFragmentManager() is undefined

getChildFragmentManager() is a method on Fragment, not on FragmentActivity. Use getSupportFragmentManager() on a FragmentActivity.

What is the difference between each constructor when extending FragmentStateAdapter?

There's actually three constructors for FragmentStateAdapter:

  • FragmentStateAdapter(FragmentActivity) - this uses the Activity's getSupportFragmentManager() and the Activity's getLifecycle(). This is what you'd use if your ViewPager2 was directly hosted within an Activity
  • FragmentStateAdapter(Fragment) - this uses the Fragment's getChildFragmentManager() and the Fragment's getLifecycle(). This is what you'd use if your ViewPager2 was hosted within another Fragment
  • FragmentStateAdapter(FragmentManager, Lifecycle) - this is what the other two constructors call internally. You wouldn't ever use this unless you were adding fragments to a service, etc. where you don't have a FragmentActivity at all.

You must always use the one that takes a Fragment (or use getChildFragmentManager()+getLifecycle() if you want to write more code for the same effect) when hosting a ViewPager2 within a Fragment - this ensures that the Fragment's your FragmentStateAdapter creates correctly have their state restored after a configuration change or process death or recreation - something that is only possible when they are child fragments of the fragment that has your ViewPager2 within it.

getChildFragmentManager() and support libraries

is there any way to get the getChildFragmentManager() method to work on devices pre-API level 17? If so, how?

Use the backport of fragments from the Android Support package (android-support-v4.jar, android.support.v4.app.Fragment).

The method does not seem to be included in either the v4 or v13 support libraries.

Yes, it is, on FragmentActivity, the Activity base class you need to use in order to use the fragments backport.

It is on the Fragment class from the backport, for managing child fragments of that fragment.

I have tried clean builds with both v4 and v13 support libraries, but that does not seem to solve the issue.

Either you did not change to FragmentActivity the Fragment backport, or your support library JARs are old. Here is a sample project demonstrating the use of FragmentActivity the Fragment backport and its getChildFragmentManager().



Related Topics



Leave a reply



Submit