Fragment Lifecycle

819 Views Asked by At

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"

2

There are 2 best solutions below

1
On

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 after onDestroy(), 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 :

if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f);
if (!f.mRetaining) {

        //performDestroy is called first if f.mRetaining is false, else not
        f.performDestroy();
        dispatchOnFragmentDestroyed(f, false);
} else {
        f.mState = Fragment.INITIALIZING;
}
//then performDetach
f.performDetach();
dispatchOnFragmentDetached(f, false);
if (!keepActive) {
        if (!f.mRetaining) {
            makeInactive(f);
        } else {
            f.mHost = null;
            f.mParentFragment = null;
            f.mFragmentManager = null;
        }
}

And here is the code of performDestroy and performDetach:

void performDestroy() {
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
        if (mChildFragmentManager != null) {
            mChildFragmentManager.dispatchDestroy();
        }
        mState = INITIALIZING;
        mCalled = false;
        mIsCreated = false;
        onDestroy();
        if (!mCalled) {
            throw new SuperNotCalledException("Fragment " + this
                    + " did not call through to super.onDestroy()");
        }
        mChildFragmentManager = null;
    }

    void performDetach() {
        mCalled = false;
        onDetach();
        mLayoutInflater = null;
        if (!mCalled) {
            throw new SuperNotCalledException("Fragment " + this
                    + " did not call through to super.onDetach()");
        }

        // Destroy the child FragmentManager if we still have it here.
        // We won't unless we're retaining our instance and if we do,
        // our child FragmentManager instance state will have already been saved.
        if (mChildFragmentManager != null) {
            if (!mRetaining) {
                throw new IllegalStateException("Child FragmentManager of " + this + " was not "
                        + " destroyed and this fragment is not retaining instance");
            }
            mChildFragmentManager.dispatchDestroy();
            mChildFragmentManager = null;
        }
    }
0
On

If you looked into whole fragment lifecycle then

onAttach() onCreate()

are counterpart of

onDetach() onDestroy()

lifecycle method respectively. So in order to not break design consistency lifecycle method calling sequence is as following

onAttach() 
onCreate()
onDestroy()
onDetach() 

now let's move on your query

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"

Android always try to maintain its counterpart. answer for your query why onAttached first gives your query's answer

Fragment is designed to be activity independent.The onAttach() provides an interface to determine the state/type/(other detail that matters to the fragment) of the containing activity with reference to the fragment before you initialize a fragment.