安卓的Dalvik时杀死这个活动Activity.onDestroy()不叫不叫、Dalvik、Activity、onDestroy

由网友(傲娇小王子)分享简介:我很困惑 Activity.onDestroy()。我需要释放一定的资源时,我的活动被破坏,但它似乎像的onDestroy()被调用正当我preSS返回键,而不是在我的活动是由Dalvik的杀害。我测试了它只是加入日志:I'm confused about Activity.onDestroy(). I need t...

我很困惑 Activity.onDestroy()。我需要释放一定的资源时,我的活动被破坏,但它似乎像的onDestroy()被调用正当我preSS返回键,而不是在我的活动是由Dalvik的杀害。我测试了它只是加入日志:

I'm confused about Activity.onDestroy(). I need to free some resources when my Activity is destroyed, but it seems like onDestroy() is called just when i press "Back" key, but not when my Activity is killed by Dalvik. I tested it just by adding log:

Log.v(my_tag,的onDestroy()被称为);

和中的onCreate相同的()方法太:

and the same in onCreate() method too:

Log.v(my_tag,的onCreate()被称为);

然后,我开始我的活动,我在日志中看到:的onCreate()被称为。我再次preSS返回键,然后开始活动,然后我看到:

Then i start my Activity, and i see in logs: onCreate() called. I press "Back" key and then start Activity again, then i see:

onDestroy() called
onCreate() called 

然后我preSSHome键,再转到我的活动,日志不会改变。所有在这里。

Then i press "Home" key and go to my Activity again, logs does not change. All right here.

然后我再preSSHome键,并开始了一些非常重的应用程序。在日志是一无所知的onDestroy(),但是当我重新开始我的活动,我在日志中看到:的onCreate()被称为!因此,的onDestroy()不叫,但我的活动被打死。怎么了?

Then i press "Home" key again and start some really "heavy" applications. In logs is nothing about onDestroy(), but when i start my Activity again, i see in logs: onCreate() called! So, onDestroy() was not called, but my Activity was killed. What's wrong?

推荐答案

在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.

在Android的,你不会得到保证的onDestroy()通话。 如果你想释放一些资源,你应该做的,在的onPause()代替。

In Android, you won't get a guaranteed onDestroy() call. If you want to release some resources, you should do that in onPause() instead.

阅读全文

相关推荐

最新文章