Fragments that are not destroyed when recreating () activity

I have a fragment of a support library that sends a network call, and recreate() parent action when I receive a specific network response. I see that the activity is actually recreated, as I see how onCreate() and onDestroy() called.

But after the activity is recreated, the fragment still exists, and it gets stuck in the loop, which continues to recreate and create new fragments.

Here is the onCreate() activity:

  if (someLogic()) { fragmentA = new FragmentA(); FragmentUtil.addFragment(getSupportFragmentManager(), fragmentA); } else { fragmentB = new FragmentB(); FragmentUtil.addFragment(getSupportFragmentManager(), fragmentB); } 

FragmentA is the one that makes the network call, and FragmentB is the fragment that should be displayed after recreate() . When I check the fragment list using getSupportFragmentManager().getFragments() , I see 1 instance of FragmentA and 16 instances of FragmentB.

My question is, why is this happening, and how to fix it?

+5
source share
2 answers

Calling recreate() essentially causes the Activity go through a configuration change. Changing the configuration causes both Activity and its Fragment destroyed and recreated. Thus, your Fragment should be destroyed when this happens (you can see for yourself by adding the Log message to your onDestroy method).

The fact that configuration changes cause the destruction and creation of fragments, however, does not mean that the FragmentManager will simply forget the fragments that have ever existed. FragmentManager will still reference the newly created fragment after the configuration change.

One thing you can do to prevent multiple fragments from being created is to do something similar in your onCreate() activity:

 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. } 
+9
source

I had the same situation as @spy, recreating the Activity after changing the Day / Night mode. Activity had four Fragments , which they get restored after calling Activity.recreate() , because the FragmentManager saves them as specified in @Alex Lockwood. But restoring and not adding Fragments after the rest does not help me because of some architectural reasons for the project, I wanted them to disappear.

The solution is to remove Fragments from the FragmentManager immediately before calling Activity.recreate() , in my case it is in Activity.onStart() :

 FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Fragment fragmentA = fragmentManager.findFragmentByTag("fragmentATag"); if(fragmentA != null) fragmentTransaction.remove(fragment); // Find and delete other Fragments fragmentTransaction.commit(); // or commitAllowingStateLoss() recreate(); 

This way you will not get the Fragments recovered when calling Activity.onCreate() .

+1
source

Source: https://habr.com/ru/post/1206577/


All Articles