Is onDestroy() guaranteed to be called for Fragments?

前端 未结 1 1761
囚心锁ツ
囚心锁ツ 2021-01-11 12:38

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

There are situations where the system will s

相关标签:
1条回答
  • 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

    0 讨论(0)
提交回复
热议问题