The problem

1. Listen for life cycle changes of fragments in the Activity

AppCompatActivity listens for changes in the Fragment’s life cycle

// Create a Fragment lifecycle callback instance
private FragmentManager.FragmentLifecycleCallbacks mFragmentLifecycleCallbacks = new FragmentManager.FragmentLifecycleCallbacks() {
    ***
}
// Listen for the Fragment life cycle in the Activity
getSupportFragmentManager().registerFragmentLifecycleCallbacks(mFragmentLifecycleCallbacks,true);
// Disable listening for the Fragment life cycle in the Activity
/ / in the onDestroy
getSupportFragmentManager().unregisterFragmentLifecycleCallbacks(mFragmentLifecycleCallbacks);
Copy the code