1》There are a few of key scenarios in which your activity is stopped and restarted:

<翻译>Activity被停止(stopped)和重启(restarted)的一些情形:

1.1》The user opens the Recent Apps window and switches from your app to another app. The activity in your app that's currently in the foreground is stopped. If the user returns to your app from the Home screen launcher icon or the Recent Apps window, the activity restarts.

1.2》The user performs an action in your app that starts a new activity. The current activity is stopped when the second activity is created. If the user then presses the Back button, the first activity is restarted.

1.3》The user receives a phone call while using your app on his or her phone.

2》Stopping Activity && Restarting Activity

Figure 1.

When the user leaves your activity, the system calls onStop() to stop the activity (1).

If the user returns while the activity is stopped, the system calls onRestart() (2), quickly followed by onStart() (3) and onResume() (4).

Notice that no matter what scenario causes the activity to stop, the system always calls onPause() before calling onStop().

When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory.

In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.

<翻译>在极端情况下,系统可能不调用最后一个生命周期函数onDestroy()就直接终止你的app进程,所以在onStop中释放可能会引起内存泄露的资源是很重要的。

Although the onPause() method is called before onStop(), you should use onStop() to perform larger, more CPU intensive shut-down operations, such as writing information to a database.

<翻译>尽管在调用onStop()方法之前会调用onPause()方法,你应该在onStop()方法执行像往数据库里写数据这样的耗时,大计算量的停止操作( larger, more CPU intensive shut-down operations)。

When your activity is stopped, the Activity object is kept resident in memory and is recalled when the activity resumes.

You don’t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state.

<翻译>不必重新初始化那些直到Resumed状态所调用的生命周期函数中所创建的组件。

The system also keeps track of the current state for each View in the layout, so if the user entered text into an EditText widget, that content is retained so you don't need to save and restore it.
<翻译>系统同时也记录布局中每个View中的当前状态,如果用户在EditText中输入信息了,那这些信息会被保留下来,所以你不必再保存和恢复它了。

Note: Even if the system destroys your activity while it's stopped, it still retains the state of the View objects (such as text in an EditText) in a Bundle (a blob of key-value pairs) and restores them if the user navigates back to the same instance of the activity (the next lesson talks more about using a Bundle to save other state data in case your activity is destroyed and recreated).

<翻译>注意:就算系统在Activity处于Stopped状态时销毁了它,系统也会在Bundle(一个键-值对)保存View的状态,并在用户返回到同一个Activity实例中时恢复它们(下一节将着重讨论使用Bundle来保存其他的状态数据以防Activity被停止和重新创建)。

3》Start/Restart Your Activity

However, because your onStop() method should essentially clean up all your activity's resources, you'll need to re-instantiate them when the activity restarts. Yet, you also need to instantiate them when your activity is created for the first time (when there's no existing instance of the activity).

For this reason, you should usually use the onStart() callback method as the counterpart to the onStop() method, because the system calls onStart() both when it creates your activity and when it restarts the activity from the stopped state.

ps:通常onStrart()方法和onStop()方法配套使用,即在onStop中释放的资源,要在onStart()方法中再初始化。

Android---Activity 生命周期(三)Stopping Activity Restarting Activity相关推荐

  1. android gilde生命周期管理,Glide原理之Activity、Fragment生命周期监听(三)

    Glide中一个重要特性是Request可以随Activity或Fragment的onStart而resume,onStop而pause,onDestroy而clear,从而节约流量和内存,并且防止内 ...

  2. activity 生命周期_如何理解安卓activity的生命周期(on-create篇)?

    个人认为用类比的方式来学习新事物比较容易接受.我这里用蝴蝶的一生来做比喻. OnCreate阶段就像是蝴蝶的幼虫刚出卵里孵化出来,蝴蝶的一生只可能出生一次,oncreate只能被创建一次.蝴蝶刚出生的 ...

  3. Android Ams对于Activity生命周期的管理

    分析Activity的生命周期管理,我觉得应该先看下面两篇关于Activity的官方文档: 了解 Activity 生命周期 处理 Activity 状态更改 里面有下面一段话,比较简洁的说出了Act ...

  4. 什么是生命周期?Activity生命周期的三种状态

    什么是生命周期 生命周期就是一个对象从创建到销毁的过程,每一个对象都有自己的生命周期.同样,Activity也具有相应的生命周期,Activity的生命周期中分为三种状态,分别是运行状态.暂停状态和停 ...

  5. Android Activity 生命周期详解及监听

    前言 系列文章: Android Activity 与View 的互动思考 Android Activity 生命周期详解及监听 Android onSaveInstanceState/onResto ...

  6. Android Activity生命周期管理

    http://blog.csdn.net/thl789/article/details/6628463 本文描述Android中Activity的状态,Activity的状态转换,从而总结了Activ ...

  7. Android:week 9总结 Activity生命周期

    目录 Monday:Activity生命周期 Tuesday Monday:Activity生命周期 1. package com.example.activity;import androidx.a ...

  8. 安卓学习笔记06:Activity生命周期与启动模式

    文章目录 零.学习目标 一.Activity生命周期 1.了解Activity生命周期 2.Activity生命周期简化图 (1)Activity存在与否 (2)Activity可见与否 (3)Act ...

  9. Activity生命周期(Activity弹出Dialog会触发Activity生命周期吗)

    Activity弹出Dialog对生命周期有什么影响_weixin_43976036的博客-CSDN博客_activity弹出dialog生命周期Activity弹出 Dialog 对生命周期有什么影 ...

  10. Android开发笔记(三十九)Activity的生命周期

    与生命周期有关的方法 下面是Activity类与生命周期有关的方法: onCreate : 创建页面 onStart : 开始页面 onStop : 停止页面 onResume : 恢复页面 onPa ...

最新文章

  1. 提速20倍!谷歌AI发布TensorFlow 3D
  2. NEO从源码分析看NEOVM
  3. OIer同样是音乐家
  4. P1282 多米诺骨牌 (差值DP+背包)
  5. 解决chrome安装扩展插件导入.crx文件提示:程序包无效的错误
  6. C++实现connected component连通分量(附完整源码)
  7. 前端学习(2062):vue的option选项
  8. 计算机图形学图形旋转_计算机图形学中的平板显示
  9. 机械原理c语言程序,c语言机械原理编程,连杆运动分析图线.docx
  10. 软件系统测试报告范文,软件系统测试报告模板.docx
  11. 10款实用苹果Siri快捷指令分享
  12. LaTeX 插入图片 公式
  13. 【跨端应用】—— uniapp黑马商城App学习笔记(二)
  14. Finalshell反复提示输入密码
  15. 计算机专业助我成长作文600,科技伴随我成长作文
  16. linux中grep -E参数,linux中grep命令
  17. Python改变图片像素值
  18. 联通软研院2020年球季校招笔试第三题 20190916
  19. java l1是啥意思_L1-020 帅到没朋友 (20分) Java
  20. FT2232H编程流程分析

热门文章

  1. [转]PHP程序中的汉字编码探讨
  2. Entity Framework 6 Alpha 3为Code First提供对存储过程支持,并提供连接恢复功能
  3. 如何修改CMD命令行窗口下的默认路径
  4. TEAM WORK 認清自己的角色
  5. 如何学习Linux性能优化?
  6. Laravel 日期时间处理包 Carbon 的应用
  7. Python3.4 django使用mysql
  8. 系统架构设计师教程学习随笔 (计算机与网络基础知识--操作系统基础知识)
  9. 线性代数矩阵论——矩阵的基本运算——加、减、取负、乘、数乘、转置
  10. boost库shared_ptr实现桥接模式