Is onDestroy called only if you explicitly call finish() ?? or are there any exceptions?

只谈情不闲聊 提交于 2019-12-08 05:20:48

问题


I have a LocalBroadcastReceiver and I am unregistering it in my ondestroy().

Now i read about ondestroy() mentioned in these two SO answers is-ondestroy-not-always-called and why-implement-ondestroy-if-it-is-not-guaranteed-to-be-called and as well as in Androi Docs that

onDestroy will be called if you explicitly call finish();

But why in my case I am not calling finish() but still ondestroy() is getting called everytime in all of my Android devices. Also according to you guys where are the cases where ondestroy() not get called up.

Also even if Android will kill my app(due to less memory) I don't need to worry as Android is going to kill my whole app so receiver will ultimately get killed.(So there won't be any Memory Leak).

So for my use case which cases are there where ondestroy() is not going to get called up.


回答1:


But why in my case I am not calling finish() but still ondestroy() is getting called everytime in all of the Android devices.

The default implementation of onBackPressed() — what is triggered by the BACK button — calls finish().

where are the cases where ondestroy() not get called up.

  1. If you crash with an unhandled exception

  2. If your process is terminated in an urgent fashion (e.g., the system needs RAM to process an incoming phone call)

  3. If the user clicks "Force Stop" on your app's screen in Settings

  4. On a few devices, if the user terminates your process using a manufacturer-supplied task manager

Also even if Android will kill my app(due to less memory) I don't need to worry as Android is going to kill my whole app so receiver will ultimately get killed

Yes, because your entire process will be terminated.



来源:https://stackoverflow.com/questions/45867605/is-ondestroy-called-only-if-you-explicitly-call-finish-or-are-there-any-exc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!