本文介绍了安卓的Dalvik时杀死这个活动Activity.onDestroy()不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑 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()文档:

[..]在有些情况下,系统只会杀死活动的  托管过程中无需调用这个方法(或任何其他),所以  它不应该被用来做的事情,是为了保持周围  经过进程消失。

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

这篇关于安卓的Dalvik时杀死这个活动Activity.onDestroy()不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:40