Will activity run onDestroy when system kill it?

房东的猫 提交于 2019-12-08 02:43:48

问题


I am wondering that will activity run the method "onDestroy" when killed by system?

for example, when the state of "activity A" is onStop ( user may press the Home button directly ), at the same time, system find out that the memory is not enough so system have to kill some background processes to keep foreground activity alife, say system kill activity A.

Will activity A run the method "onDestroy" in this situation?


回答1:


It will purely depend on the system condition at that time. Docs clearly says about onDestroy() that:

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.

See Here




回答2:


From the developer.android.com :

When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory. In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.

So, android usually will call onDestroy() of your activity before it is killed but it is not guaranteed.

Link : http://developer.android.com/training/basics/activity-lifecycle/stopping.html




回答3:


Depends, as when system kills an application, it's associated PID killed by it directly. As Android is nothing but Linux, it sends SIG9 (9 number signal is "kill")/ kill (Application's PID) to kill application without invoking it's callback methods.



来源:https://stackoverflow.com/questions/18051137/will-activity-run-ondestroy-when-system-kill-it

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