Android:当 Dalvik 杀死此 Activity 时,不会调用 Activity.onDestroy()
我对 Activity.onDestroy() 感到困惑。当我的 Activity 被销毁时,我需要释放一些资源,但似乎 onDestroy() 是在我按“后退”键时调用的,而不是当我的 Activity 被 Dalvik 杀死时调用的。我只是通过添加日志来测试它:
Log.v("my_tag", "onDestroy() called");
以及 onCreate() 方法中的相同内容:
Log.v("my_tag ", "onCreate() 被调用");
然后我启动我的 Activity,我在日志中看到:onCreate() 被调用
。我按“后退”键,然后再次启动活动,然后我看到:
onDestroy() called
onCreate() called
然后我按“主页”键并再次转到我的活动,日志没有更改。好吧在这里。
然后我再次按“Home”键并启动一些非常“重”的应用程序。日志中没有关于 onDestroy() 的信息,但是当我再次启动 Activity 时,我在日志中看到:onCreate() 调用
!所以, onDestroy() 没有被调用,但我的 Activity 被杀死了。怎么了?
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() called");
and the same in onCreate() method too:
Log.v("my_tag", "onCreate() called");
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
Then i press "Home" key and go to my Activity again, logs does not change. All right here.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 onDestroy() 文档:
在 Android 中,您不会得到有保证的
onDestroy()
调用。如果您想释放一些资源,您应该在
onPause()
中执行此操作。From the onDestroy() documentation:
In Android, you won't get a guaranteed
onDestroy()
call.If you want to release some resources, you should do that in
onPause()
instead.