Is onDestroy() guaranteed to be called for Fragments?
Asked Answered
V

1

15

I know for an Activity onDestroy(...) is not guaranteed to be called. According to the docs,

There are situations where the system will simply kill the activity's hosting process without >calling this method (or any others) in it, so it should not be used to do things that are >intended to remain around after the process goes away.

Does this also apply to Fragments? Nothing is stated in the docs but just want to make sure.

Vasoconstrictor answered 10/2, 2014 at 3:16 Comment(0)
O
12

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

Occasional answered 10/2, 2014 at 3:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.