Activity.onStop() 是否保证被调用(API 11 +) [英] Is Activity.onStop() guaranteed to be called (API 11 +)

查看:22
本文介绍了Activity.onStop() 是否保证被调用(API 11 +)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档中不清楚(至少对我而言)是否保证调用 Activity.onStop.有两个地方的信息看似矛盾.

  1. Activity.onStop 的 Javadoc:

<块引用>

请注意,在内存不足的情况下,系统可能永远不会调用此方法,即在调用 onPause() 方法后系统没有足够的内存来保持您的 Activity 进程运行.

  1. Activity 类的文档(特别是Killable"列)

    来源 3:

    It is unclear (to me, at least) from the documentation whether Activity.onStop is guaranteed to be called. There are two places with seemingly contradictory information.

    1. Javadoc for Activity.onStop:

    Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

    1. Documentation (in particular 'Killable' column) for Activity class http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle:

    Starting with Honeycomb, an application is not in the killable state until its onStop() has returned. This impacts when onSaveInstanceState(Bundle) may be called (it may be safely called after onPause() and allows and application to safely wait until onStop() to save persistent state.

    It's a bit of a struggle to find a way that both pieces of documentation are telling the truth. The only scenario I can think of is this: Suppose you are developing on target API 21 (with min sdk 10) and write an Activity with an onStop() method. If you then run this application on an API 10 phone, onStop() is not guaranteed to be called. This scenario means that the documentation from points 1. and 2. above are both true. Otherwise, one of them must be false.

    解决方案

    Is Activity.onStop() guaranteed to be called (API 11 +)

    Yes, it is guaranteed to be called on post-Honeycomb devices (API 11 +)

    Source 1: Video tutorial on Activity life cycle - taught by Google developer advocate

    Source 2: https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

    Source 3: AndroidLifeCycle article on www.vogella.com

    这篇关于Activity.onStop() 是否保证被调用(API 11 +)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆