释放onPause而不是onDestroy中的资源

Cap*_*ous 11 android activity-lifecycle android-lifecycle

这是关于POST-honeycomb(即Android 3.0+),以下引用来自https://developer.android.com/reference/android/app/Activity.html

根据生命周期,onStop和onDestroy是可以杀死的,这意味着:

请注意上表中的"Killable"列 - 对于那些标记为可填充的方法,在该方法返回之后,托管该活动的进程可能在任何时候被系统杀死而不执行其代码的另一行

  1. 换句话说,的onStop(沿此事件之前发生的其他人)是保证被调用,但此刻的方法返回时,过程可能会死,所以的onDestroy是不能保证被调用.

    另一句话说:

    对于那些未标记为可填充的方法,系统不会从调用方法开始并在返回后继续执行活动的进程.

    其次是

    因此,活动处于可填充状态,例如,在onPause()之后到onResume()的开始之间.

  2. 但这并不符合什么上面所说的,除非这只相当于预蜂窝.对于POST蜂窝,情况并非如此,对吧?所以基本上,onPause和onStop都可以保证被调用.

  3. 假设我只在onDestroy中释放一个资源,那么这可能会导致泄漏,因为可能不会调用onDestroy,对吧?

  4. 然而,可以在此情况下(即的onDestroy不叫)出现,除了当过程由机器人本身被杀?是否有任何其他方案导致不调用onDestroy,从而泄漏资源.

  5. 当Android杀死资源将被破坏并且不会发生泄漏(即使我们没有明确释放资源吗?)这是真的吗?

请提供详细资料,说明这些陈述(1)(2)(3)(4)(5)是否正确.

Vas*_*liy 15

首先,让我们了解您引用的文档会发生什么.

以下命令显示AOSP中git blame的Activity.java文件输出:

$ cd $AOSP/frameworks/base
$ git blame ./core/java/android/app/Activity.java
Run Code Online (Sandbox Code Playgroud)

输出的相关部分:

9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  363)  * <p>Note the "Killable" column in the above table -- for those methods that
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  364)  * are marked as being killable, after that method returns the process hosting the
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  365)  * activity may killed by the system <em>at any time</em> without another line
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  366)  * of its code being executed.  Because of this, you should use the
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  367)  * {@link #onPause} method to write any persistent data (such as user edits)
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  368)  * to storage.  In addition, the method
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  369)  * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  370)  * in such a background state, allowing you to save away any dynamic instance
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  371)  * state in your activity into the given Bundle, to be later received in
550116576 (RoboErik                        2014-07-09 15:05:53 -0700  372)  * {@link #onCreate} if the activity needs to be re-created.
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  373)  * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  374)  * section for more information on how the lifecycle of a process is tied
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  375)  * to the activities it is hosting.  Note that it is important to save
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  376)  * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
5c40f3fcc (Daisuke Miyakawa                2011-02-15 13:24:36 -0800  377)  * because the latter is not part of the lifecycle callbacks, so will not
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  378)  * be called in every situation as described in its documentation.</p>
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  379)  *
0aae2d4e0 (Dianne Hackborn                 2010-12-07 23:51:29 -0800  380)  * <p class="note">Be aware that these semantics will change slightly between
0aae2d4e0 (Dianne Hackborn                 2010-12-07 23:51:29 -0800  381)  * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
0aae2d4e0 (Dianne Hackborn                 2010-12-07 23:51:29 -0800  382)  * vs. those targeting prior platforms.  Starting with Honeycomb, an application
0aae2d4e0 (Dianne Hackborn                 2010-12-07 23:51:29 -0800  383)  * is not in the killable state until its {@link #onStop} has returned.  This
0aae2d4e0 (Dianne Hackborn                 2010-12-07 23:51:29 -0800  384)  * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
0aae2d4e0 (Dianne Hackborn                 2010-12-07 23:51:29 -0800  385)  * safely called after {@link #onPause()} and allows and application to safely
0aae2d4e0 (Dianne Hackborn                 2010-12-07 23:51:29 -0800  386)  * wait until {@link #onStop()} to save persistent state.</p>
0aae2d4e0 (Dianne Hackborn                 2010-12-07 23:51:29 -0800  387)  *
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  388)  * <p>For those methods that are not marked as being killable, the activity's
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  389)  * process will not be killed by the system starting from the time the method
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  390)  * is called and continuing after it returns.  Thus an activity is in the killable
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  391)  * state, for example, between after <code>onPause()</code> to the start of
9066cfe98 (The Android Open Source Project 2009-03-03 19:31:44 -0800  392)  * <code>onResume()</code>.</p>
Run Code Online (Sandbox Code Playgroud)

请注意,Dianne Hackborn在2010-12-07中添加了讨论蜂窝后行为的段落,而附带的段落可以追溯到2009-03-03.

它告诉我们的是Dianne添加了新段落而没有更新javadoc的其余部分,因此是矛盾.不幸的是,这在Android中并不罕见.

对你的问题:

1)在Android两个蜂窝后的版本onResume(),并onStop()保证被称为(在她除了活动的Javadoc由戴安娜Hackborn说明).

2)在预蜂蜜之前,只onPause()保证被调用(如早期版本的Activity的javadoc所述)

onDestroy()只有托管整个应用程序的进程被终止时才会调用3,4,5).当进程被终止时,分配给它的所有资源都被释放,因此在这种情况下不存在内存泄漏的风险.

重要说明:由于释放资源onDestroy()不会导致内存泄漏,因此将所有"释放"代码放在那里似乎是个好主意.但是,它很少是最佳方法.为什么?参见下文.

当Activity进入后台时,它会停止,但不会被破坏(通常).Activity可以长时间保持这种"停止"状态,并且如果用户返回应用程序将再次启动.如果您释放资源onDestroy()(默认情况下在Activity进入后台时未调用),Activity则会在处于停止状态时保留这些资源,从而导致应用程序在后台状态下消耗更多资源.

当Android内存不足时,它会开始终止进程​​以释放它们消耗的内存.在选择要杀死哪些进程时考虑的最重要的考虑因素之一是它们的资源消耗.因此,如果您的应用程序在处于后台停止状态时保留资源,则将有更高的机会被Android杀死.

此外,我们开发人员必须确保为用户制作最好的应用.在后台使用非最少量的用户电话资源和电池的应用程序不是一个好的应用程序.用户会知道它!

因此,我强烈建议释放onStop()方法中的所有资源.我通常不会覆盖onDestroy()方法Activities,并Fragments在所有.

推论:正如@Juan在评论中所指出的,上述重要说明具有同样重要但不那么明显的必然结果:onStart()应该是分配资源的唯一方法.无论您对"资源"的定义是什么,既不onCreate()也不onResume()应该分配这些资源.

  • 保持代码一致的另一个考虑因素.在onCreate()中获取的资源应该在onDestroy()中释放.onResume()中获取的资源应该在onPause()中释放.onStart() - onStop()也是如此. (2认同)