Android 关于生命周期回调 onPause() 和 onStop() 的矛盾文档

标签 android android-activity android-lifecycle

直觉上,最适用的回调函数是onPause()。然而,文档中似乎存在矛盾:

根据 https://developer.android.com/guide/components/activities/activity-lifecycle.html :

onPause() execution is very brief, and does not necessarily afford enough time to perform save operations. For this reason, you should not use onPause() to save application or user data, make network calls, or execute database transactions; such work may not complete before the method completes. Instead, you should perform heavy-load shutdown operations during onStop().

You should also use onStop() to perform relatively CPU-intensive shutdown operations. For example, if you can't find a more opportune time to save information to a database, you might do so during onStop().

根据 https://developer.android.com/reference/android/app/Activity.html :

Note the "Killable" column in the above table -- for those methods that are marked as being killable, after that method returns the process hosting the activity may be killed by the system at any time without another line of its code being executed. Because of this, you should use the onPause() method to write any persistent data (such as user edits) to storage.

那么,它应该在哪里完成以及在另一个生成的线程上完成?这通常是如何完成的?

最佳答案

抓得好!

总结几点:

  • 不要将 onPause() 用于真正重负载关机操作​​”,否则您通过放慢“在该方法返回之前要恢复的下一个 Activity ”来获得负面用户体验的风险。在其他情况下继续这样做;
  • 如果您仍然犹豫,请使用 onSaveInstanceState() .它在 onPause()onStop() 之间调用,据我所知,一定会被调用。系统依赖于方法“这样当一个 Activity 被杀死时,系统可以恢复它的状态,在未来回到它”
  • 关于 onStop(),就我个人而言,我从未!经历过在方法返回之前终止我的应用进程的经历,但看到如此说明的问题,据我们所知,对应官方文档。但是,真的非常难得。因此,是否依赖它取决于您。

关于Android 关于生命周期回调 onPause() 和 onStop() 的矛盾文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43414648/

相关文章:

android - 进程被终止时切勿调用 Create

Android 服务仅在 UI 处于 Activity 状态时运行

android - 在 android.R.layout.simple_list_item_1 上使用 butterknife 绑定(bind) View

android - 我应该在 BindAdapter 类中使用 inject 吗?

Android最佳实践——Activity与Fragment之间的通信

java - 清除 Activity 堆栈

android - 看不见的音频文件

ExpandableListView 组项目上的 Android LongClickListener

android - Android Activity 中 onCreate 方法第二个实现有什么用?

android - 连接到 Google API 客户端时 Android Activity 生命周期的奇怪行为