I have a support library fragment that sends a network call, and recreate()
the parent activity when it receives a specific network response. I can see that the activity does get recreated as I can see the onCreate()
and onDestroy()
are called.
But after the activity is recreated, the fragment is still there and it got stuck in a loop which keep recreating and making new fragments.
Here's part of the onCreate()
of the activity:
if (someLogic()) {
fragmentA = new FragmentA();
FragmentUtil.addFragment(getSupportFragmentManager(), fragmentA);
} else {
fragmentB = new FragmentB();
FragmentUtil.addFragment(getSupportFragmentManager(), fragmentB);
}
FragmentA is the one that does the network call, and FragmentB is the fragment that should be displayed after the recreate()
. When I check the list of fragments with getSupportFragmentManager().getFragments()
I see 1 instances of FragmentA, and 16 instances of FragmentB.
My question is why does this happen, and how do I fix it?
A paused Fragment is still alive (all state and member information is retained by the system), but it will be destroyed if the Activity is destroyed. If the user presses the Back button and the Fragment is returned from the back stack, the lifecycle resumes with the onCreateView() callback.
onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity. onDestroyView() allows the fragment to clean up resources associated with its View. onDestroy() called to do final cleanup of the fragment's state.
Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment's view hierarchy becomes part of, or attaches to, the host's view hierarchy. Note: Some Android Jetpack libraries, such as Navigation, BottomNavigationView , and ViewPager2 , are designed to work with fragments.
Activity is an application component that gives a user interface where the user can interact. The fragment is only part of an activity, it basically contributes its UI to that activity.
Calling recreate()
essentially causes the Activity
to go through a configuration change. A configuration change causes both the Activity
and its Fragment
s to be destroyed and recreated. So your Fragment
s should in fact be destroyed when this occurs (you can see for yourself by adding a Log
message in your fragment's onDestroy
method).
The fact that configuration changes cause fragments to be destroyed and created however does not mean that the FragmentManager
will simply forget the fragments ever existed. The FragmentManager
will still hold a reference to the newly created fragment after the configuration change occurs.
One thing you can do to prevent multiple fragments from being created is by doing something like this in your activity's onCreate()
:
if (savedInstanceState == null) {
// The Activity is being created for the first time, so create and
// add new fragments.
} else {
// Otherwise, the activity is coming back after being destroyed.
// The FragmentManager will restore the old Fragments so we don't
// need to create any new ones here.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With