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

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

问题描述

目前还不清楚(对我来说,至少)从Activity.onStop是否有保证的文件被调用。有两个地方看似矛盾的信息。

  1. 的Javadoc Activity.onStop:
  

请注意,此方法可能永远不会被调用,在内存不足的情况​​下,该系统没有足够的内存,让您的活动的进程中运行它的onPause()方法被调用后。

<醇开始=2>
  • 文档(尤其是Killable列)活动类的http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle:
  •   

    与蜂窝启动,应用程序是不是在killable状态,直到它的onStop()返回。这种影响时的onSaveInstanceState(包)可被称为(它可以安全地调用的onPause(后),并允许应用安全地等到的onStop()来保存持久状态。

    这是一个有点斗争,发现两件文件说的是真话的方式。唯一的情况下我能想到的是这样的:假设你正在开发目标API 21(含分钟SDK 10),并就活动与的onStop()方法。如果再上一个API 10手机运行该应用程序,的onStop()不能保证被调用。这种情况意味着,从点1和2的文档上面都是真实的。否则,他们中的一个肯定是假的。

    解决方案

    如果你再往下的的 ActivityLifeCycle参考,它说:

      

    对于那些没有标记为killable方法,活动的过程中不会被从时间启动系统杀死了方法被调用并继续它返回之后。因此,一个活动是在killable状态,例如,之间经过的onPause(),以onResume开始()

    由于这似乎重新有效保单点#1,我宁可保守的设计和规划我的code,如果点#1是真实的,而忽略点#2。

    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.

    解决方案

    If you look further down in the ActivityLifeCycle reference, it says:

    For those methods that are not marked as being killable, the activity's process will not be killed by the system starting from the time the method is called and continuing after it returns. Thus an activity is in the killable state, for example, between after onPause() to the start of onResume().

    Since this appears to re-inforce point #1, I would err on the conservative side and design and plan my code as if point #1 were true and ignore point #2.

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

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