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'sgetSupportFragmentManager()
and the Activity'sgetLifecycle()
. This is what you'd use if yourViewPager2
was directly hosted within an ActivityFragmentStateAdapter(Fragment)
- this uses the Fragment'sgetChildFragmentManager()
and the Fragment'sgetLifecycle()
. This is what you'd use if yourViewPager2
was hosted within another FragmentFragmentStateAdapter(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 aFragmentActivity
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 the FragmentActivity
Fragment
backport, or your support library JARs are old. Here is a sample project demonstrating the use of the FragmentActivity
Fragment
backport and its getChildFragmentManager()
.
Related Topics
Android and Facebook Share Intent
How to Sort Firebase Records by Two Fields (Android)
How to Animate Recyclerview Items When They Appear
Can Not Find a View With Findviewbyid()
How to Find Out Which Keystore Was Used to Sign an App
View Contents of Database File in Android Studio
How to Read Mms Data in Android
Httpclient Won't Import in Android Studio
What's the Best Way to Limit Text Length of Edittext in Android
How to Find Serial Number of Android Device
How to Send a File in Android from a Mobile Device to Server Using Http
How to Use Arrayadapter≪Myclass≫
How to Get Uri from an Asset File
How to Get Clickable Hyperlinks in Alertdialog from a String Resource