2

我想知道活动被系统杀死时会运行“onDestroy”方法吗?

例如,当“activity A”的状态为 onStop(用户可以直接按下 Home 键)时,同时系统发现内存不足,系统不得不杀死一些后台进程以保持前台 Activity 存活,假设系统终止活动 A。

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

4

3 回答 3

3

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

于 2013-08-05T05:40:06.497 回答
1

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

于 2013-08-05T05:39:58.477 回答
0

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

于 2013-08-05T05:48:22.707 回答