【发布时间】:2017-11-20 08:15:18
【问题描述】:
我想知道我的应用在后台时是否被杀死。我正在使用 android nougat 设备进行测试。但是当我通过滑动杀死我的应用程序时,我的服务的 onTaskRemoved() 没有调用。
有什么方法可以知道用户在调用的活动的 onDestroy() 之前何时杀死了应用程序?
【问题讨论】:
-
当应用程序在后台并且由于内存不足而被操作系统杀死时,操作系统会杀死应用程序并调用应用程序的onDestroy。就我对生命周期的了解而言,通过滑动应用程序来杀死应用程序,不会调用回调方法。
-
不,如果你的整个进程都被杀死了,只有刺客知道这一点
-
@AbdulWaheed
android.app.Application没有onDestroy方法 -
@pskink 是的,我知道这一点,我牢记这一点,如果应用程序在后台,则必须将某些活动加载到进程的内存中。并且该活动的 onDestroy 将被调用。对不对?
-
@AbdulWaheed 不,
Activity#onDestroy文档说:"Note: do not count on this method being called as a place for saving data! [...] 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 activity-lifecycle