I'm trying to understand the internal behavior of Android fragments. Got doubts between exact difference between onDestroy()
, onDetach()
and
void onDestroy ()
Called when the fragment is no longer in use. This is called after onStop()
and before onDetach().
void onDetach ()
Called when the fragment is no longer attached to its activity. This is called after onDestroy().
Query : If fragment is not longer in use,means that we can remove that fragment from Activity right?
In this case why to call onDestroy () first then onDetach () next,We can use only one method to indicate the state that "Fragment is no longer in use,can be removed activity"
onDestroy() :
onDestroy()
called to do final clean up of the fragment’s state but Not guaranteed to be called by the Android platform. (Called when the fragment is no longer in use, after onStop and before onDetach())onDetach() :
onDetach()
called afteronDestroy()
, to notify that the fragment has been disassociated from its hosting activity. (Called when the fragment is no longer attached to its activity)ref: android-fragment-lifecycle, onDestroy,onDetach
take a look at Fragment class (line 1564), performDestroy is called first if f.mRetaining is false :
And here is the code of performDestroy and performDetach: