Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments not destroyed when recreate() activity

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?

like image 523
Cash Lo Avatar asked Nov 10 '14 17:11

Cash Lo


People also ask

Are fragments destroyed when activity is destroyed?

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.

What happens to fragment when activity is stopped?

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.

Can a fragment exist without an activity?

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.

What is the relationship between fragment and activity?

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.


1 Answers

Calling recreate() essentially causes the Activity to go through a configuration change. A configuration change causes both the Activity and its Fragments to be destroyed and recreated. So your Fragments 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.
}
like image 101
Alex Lockwood Avatar answered Sep 29 '22 11:09

Alex Lockwood