关于android:当系统杀死它时,活动会运行onDestroy吗?

Will activity run onDestroy when system kill it?

我想知道当系统杀死时,活动会运行方法"onDestroy"吗?

例如,当"活动A"的状态为onStop(用户可以直接按Home键)时,
同时,系统发现内存不够,所以系统必须杀死一些后台进程才能保持前台活动,系统杀死活动A.

在这种情况下,活动A会运行方法"onDestroy"吗?


它完全取决于当时的系统状况。 文档明确说明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.

看这里


来自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.

所以,android通常会在你的活动被杀之前调用它的onDestroy(),但是不能保证。

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


取决于,当系统杀死一个应用程序时,它是由它直接杀死的关联PID。 由于Android只是Linux,它发送SIG9(9号码信号为"kill")/ kill(应用程序的PID)来杀死应用程序而不调用它的回调方法。