简体   繁体   English

是否保证为片段调用onDestroy()?

[英]Is onDestroy() guaranteed to be called for Fragments?

I know for an Activity onDestroy(...) is not guaranteed to be called. 我知道不能保证调用Activity onDestroy(...) 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. 文档中没有任何内容,只是想确定。

I believe that Fragment's onDestroy() is not guaranteed to be called just as Activity's. 我相信Fragment的onDestroy()不能保证像Activity一样被调用。

In Activity's performDestroy() : 在Activity的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. 如果你深入了解源代码, mFragments.dispatchDestroy()将最终调用片段onDestroy() So, if Activity's onDestroy() not called, fragment's onDestroy() won't be called. 因此,如果未调用Activity的onDestroy()不会调用片段的onDestroy()

And there's some other links: 还有一些其他链接:

fragment lifecycle: when "ondestroy" and "ondestroyview" are not called? 片段生命周期:何时不调用“ondestroy”和“ondestroyview”?

Android fragments lifecycle onStop, onDestroyView, onDestroy and onDetach Android片段生命周期onStop,onDestroyView,onDestroy和onDetach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM