Fragment Lifecycle: When "Ondestroy" and "Ondestroyview" Are Not Called

fragment lifecycle: when ondestroy and ondestroyview are not called?

Take a look at this question: Why implement onDestroy() if it is not guaranteed to be called?

Basically, onDestroy() is only guaranteed to be called if you call finish(). Otherwise, onDestroy() may not be called until the system deems it necessary. You might want to look at putting your "closing" logic in the onPause() or onStop() instead.

When would Fragment's onDestroyView be called, but it wouldn't be destroyed?

It seems to all depend on whether the fragment is retained or not. When the fragment is retained, then after onDestroyView comes onCreateView.

When the fragment is retained (i.e. setRetainInstance(true)), then the log while rotating the devicelooks like this:

com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreate
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onPause
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStop
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDestroyView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDetach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume

But when it is not retained, it goes like this:

com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreate
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onPause
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStop
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDestroyView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDestroy
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDetach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreate
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume

Fragment's onDestroyView not called on back button press

I believe there is some misunderstanding of the back stack.
When you replace A with B, you put the transaction A->B in the back stack, not the A fragment. The back stack knows that when the user presses the back button, it will have to rollback the transaction: destroy B and recreate A. In your case, you are replacing C with B and you're pressing back button: A will be recreated and B cannot be destroyed since it doesn't exist.
Maybe you can find a solution listening for back stack events using FragmentManager.addOnBackStackChangedListener(), but I don't know if it fits your requirement.

Understanding Fragment's lifeCycle methods calls during fragment transaction

Does this mean that no method of current fragment is called when new fragment is added in same activity?

Correct, your first fragment A will only be affected if it's removed or replaced (case 2).
Simply adding another fragment will just display fragment B over fragment A and no life cycle callbacks should be called.

What i expected was?

onStart method of Fragment A is called since Fragment A is visible now

Again, since fragment B was added on top of A, fragment A is not affected by the removal of B.

onDestroy and onDetach method of Fragment A is NOT called.Why its not called?Bcoz as per documentation method replace removes any fragments that are already in the container and add your new one to the same container

Unlike a simple replace, when you add your replace transaction to the backstack you're actually keeping the first fragment attached to it's activity, only its view is destroyed.

Once you pop the backstack fragment B is removed and fragment A will just recreate its view - starting from onCreateView().

Android Fragment onCreateView after onDestroy not called again

After investigating some time i finally "solved" the problem by creating the view in onCreateView and destroy it in onDestroyView, without understanding why the system does not call the callback as described in the sdk documentation.

Fragment's onDestroyView called but view is not being destroyed

Fragments in back stack retain their state in a Bundle. This includes the view hierarchy state with the current contents of EditTexts and such.

Is onDestroy() guaranteed to be called for Fragments?

I believe that Fragment's onDestroy() is not guaranteed to be called just as Activity's.

In Activity's performDestroy():

 final void performDestroy() {
mDestroyed = true;
mWindow.destroy();
mFragments.dispatchDestroy();
onDestroy();
if (mLoaderManager != null) {
mLoaderManager.doDestroy();
}
}

where mFragments.dispatchDestroy() will finally call fragments onDestroy(), if you digg into the source. So, if Activity's onDestroy() not called, fragment's onDestroy() won't be called.

And there's some other links:

fragment lifecycle: when "ondestroy" and "ondestroyview" are not called?

Android fragments lifecycle onStop, onDestroyView, onDestroy and onDetach



Related Topics



Leave a reply



Submit