activity 处于暂停状态时会发生什么情况?
what happens to an activity when in pause state?
当 Activity
进入暂停状态时,它的一个实例将保留在由操作系统管理的 Activity
堆栈中。我知道过了一会儿,它会破坏实例。现在我的问题是当操作系统在很长一段时间后销毁Activity
实例时是否调用了onDestroy()
方法?
就像我在 onDestroy()
方法中放置一个 Toast
一样,当 Activity
实例被 OS 销毁时会显示吗? (我知道它会通过按返回按钮显示)。
这个问题的性质使其难以测试,因为有时 OS 需要一天或更长时间才能销毁堆栈中的 Activity
个实例。
不保证它会被调用。你可以看到Activity#onDestroy。
Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. 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.
通过简单地杀死它可能是例如System.exit
调用或类似的跳过生命周期挂钩的东西。
当 Activity
进入暂停状态时,它的一个实例将保留在由操作系统管理的 Activity
堆栈中。我知道过了一会儿,它会破坏实例。现在我的问题是当操作系统在很长一段时间后销毁Activity
实例时是否调用了onDestroy()
方法?
就像我在 onDestroy()
方法中放置一个 Toast
一样,当 Activity
实例被 OS 销毁时会显示吗? (我知道它会通过按返回按钮显示)。
这个问题的性质使其难以测试,因为有时 OS 需要一天或更长时间才能销毁堆栈中的 Activity
个实例。
不保证它会被调用。你可以看到Activity#onDestroy。
Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. 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.
通过简单地杀死它可能是例如System.exit
调用或类似的跳过生命周期挂钩的东西。