简体   繁体   English

系统杀死后,活动会在onDestroy上运行吗?

[英]Will activity run onDestroy when system kill it?

I am wondering that will activity run the method "onDestroy" when killed by system? 我想知道活动在被系统杀死后会运行方法“ onDestroy”吗?

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. 例如,当“活动A”的状态为onStop(用户可以直接按“主页”按钮)时,系统会发现内存不足,因此系统必须杀死一些后台进程以保持前台活动的生命,说系统杀死活动A。

Will activity A run the method "onDestroy" in this situation? 在这种情况下,活动A是否会运行方法“ onDestroy”?

It will purely depend on the system condition at that time. 这将完全取决于当时的系统状况。 Docs clearly says about onDestroy() that: Docs关于onDestroy()明确指出:

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 这里

From the developer.android.com : 来自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. 因此,Android通常会在您的活动被终止之前调用其活动的onDestroy(),但不能保证。

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

Depends, as when system kills an application, it's associated PID killed by it directly. 取决于,当系统终止应用程序时,它直接与应用程序关联的PID被终止。 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. 由于Android只不过是Linux而已,因此它发送SIG9 (9号信号表示“ kill”)/ kill(应用程序的PID)终止应用程序,而无需调用其回调方法。

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

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