本节要点:paused和stop状态下的生命周期和该生命周期应该干的事情。(需要注意的是在onStop方法中释放的资源,如果Activity重新进入onResume,必须重新初始化)

//===================================================================================

During normal app use, the foreground activity is sometimes obstructed by other visual components that cause the activity to pause. For example, when a semi-transparent activity opens (such as one in the style of a dialog), the previous activity pauses. As long as the activity is still partially visible but currently not the activity in focus, it remains paused.

However, once the activity is fully-obstructed and not visible, itstops (which is discussed in the next lesson).

As your activity enters the paused state, the system calls theonPause() method on your Activity, which allows you to stop ongoing actions that should not continue while paused (such as a video) or persist any information that should be permanently saved in case the user continues to leave your app. If the user returns to your activity from the paused state, the system resumes it and calls the onResume() method.

当Activity处于半透明状态(比如Activity界面显示了一个对话框),那么Activity调用onPause方法;当用户从  paused state返回activity 的时候,调用onResume方法。

Note: When your activity receives a call to onPause(), it may be an indication that the activity will be paused for a moment and the user may return focus to your activity. However, it's usually the first indication that the user is leaving your activity.

//=================================================================================================================

Pause Your Activity

When the system calls onPause() for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity and it will soon enter the Stopped state. You should usually use the onPause() callback to:

  • Stop animations or other ongoing actions that could consume CPU.
  • Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
  • Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.
@Override
public void onPause() {super.onPause();  // Always call the superclass method first// Release the Camera because we don't need it when paused// and other activities might need to use it.if (mCamera != null) {mCamera.release()mCamera = null;}
}

Generally, you should not use onPause() to store user changes (such as personal information entered into a form) to permanent storage. The only time you should persist user changes to permanent storage withinonPause() is when you're certain users expect the changes to be auto-saved (such as when drafting an email). However, you should avoid performing CPU-intensive work during onPause(), such as writing to a database, because it can slow the visible transition to the next activity (you should instead perform heavy-load shutdown operations during onStop()).

You should keep the amount of operations done in the onPause() method relatively simple in order to allow for a speedy transition to the user's next destination if your activity is actually being stopped.

Note: When your activity is paused, the Activity instance 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.

//==================================================================================================

Resume Your Activity

When the user resumes your activity from the Paused state, the system calls the onResume() method.

Be aware that the system calls this method every time your activity comes into the foreground, including when it's created for the first time. As such, you should implement onResume() to initialize components that you release during onPause() and perform any other initializations that must occur each time the activity enters the Resumed state (such as begin animations and initialize components only used while the activity has user focus).

The following example of onResume() is the counterpart to the onPause() example above, so it initializes the camera that's released when the activity pauses.

@Override
public void onResume() {super.onResume();  // Always call the superclass method first// Get the Camera instance as the activity achieves full user focusif (mCamera == null) {initializeCamera(); // Local method to handle camera init}
}

06Pausing and Resuming an Activity相关推荐

  1. [Android Training视频系列]2.2 Pausing and Resuming an Activity

    [Android Training视频系列]2.2 Pausing and Resuming an Activity 1.主要内容 本讲介绍onPause和onResume,主要分析了在onPause ...

  2. Pausing and Resuming an Activity 暂停和恢复活动

    Pausing and Resuming an Activity 暂停和恢复活动 Previous Next Get started       This lesson teaches you to ...

  3. 【Android 插件化】Hook 插件化框架 ( 从 Hook 应用角度分析 Activity 启动流程 二 | AMS 进程相关源码 | 主进程相关源码 )

    Android 插件化系列文章目录 [Android 插件化]插件化简介 ( 组件化与插件化 ) [Android 插件化]插件化原理 ( JVM 内存数据 | 类加载流程 ) [Android 插件 ...

  4. Managing the Activity Lifecycle

    原链接:http://developer.android.com/training/basics/activity-lifecycle/index.html As a user navigates t ...

  5. (android文档原创翻译)管理Activity的生命周期一

    ( 英文原文链接地址) 1.启动你的Activity 不同其它的应用程序通过main()方法来启动,android系统是按照一定的顺序通过调用其生命周期(lifecycle)的回调方法来启动或者结束一 ...

  6. Android系统源码分析--Activity的finish过程

    上一篇我们分析了Activity的启动流程,由于代码量很大,还是没有分析的很详细,但是基本流程都出来了,更详细的东西还是要去看源码.这一章来分析Activity的finish过程,分析一下finish ...

  7. c语言mysql源代码,永盈会-官方网站

    SLF4J1.7 一个日志模板,从traceback打印异常受启发做的模板,可被pycharm esclip sublime等ide识别和跳转. 安卓构架组件--向项目添加组件(Adding Comp ...

  8. Android onPause()和onStop()区别

    Paused  谷歌官方的解释是:Another activity is in the foreground and has focus, but this one is still visible. ...

  9. Android中的一些基础知识(二)

    这几天在回顾Android的基础知识,就把一些常见的知识点整理一下,以后忘了也可以翻出来看一看. 简单介绍一下Activity的生命周期  在API文档中对生命周期回调的函数描述的很详细,这里我只是翻 ...

最新文章

  1. MySQL 8.0 Invisible Indexes 和 RDS 5.6 Invisible Indexes介绍
  2. 如何用Splunk建立可疑DNS报警系统
  3. Design Pattern in Java[Challenge 2.1]
  4. php 怎么执行unoconv,web执行php调用exec(unoconv)命令失败解决方案
  5. 帕斯卡三角形杨辉三角
  6. 字节跳动AI Lab 算法岗专属内推码!
  7. 怎样快速更新已安装的软件?
  8. 接口测试及服务器性能压测,接口测试及服务器性能压测
  9. 自主知识产权 曙光龙芯3号服务器将面市
  10. 湖北 政府项目 软件 测试,湖北电子政务应用系统技术验收测试规范.doc
  11. 已解决:“apktool” W: invalid resource directory name:XXX\res navigation
  12. ImageIO: PNG invalid PNG file: iDOT doesn't point to valid IDAT chunk
  13. 网上车管所办理驾驶证补证换证
  14. Inception模块 GooLeNet网络
  15. 第十六届“振兴杯”计算机网络管理员赛项理论参考题库(1)单选
  16. mac虚拟机改显存_虚拟机mac怎么增大显存
  17. 计算机笔记检讨,上课没做笔记的反省检讨书
  18. excel合并多个表格的快速操作
  19. vs的快捷键——注释/取消注释
  20. 进制转换与进制转换表达式

热门文章

  1. 《HFSS电磁仿真设计从入门到精通》一2.1 设计概述
  2. 最新精选绿色软件之格式转换(小熊每期更新)
  3. 编写函数unsigned int reverse_bit(unsigned int value),实现无符号二进制整数在32位机器上的bit位翻转
  4. (附源码)计算机毕业设计SSM建筑工地环保监控系统研究
  5. 非线性悬架matlab,基于MATLAB/Simulink的油气悬架非线性阻尼特性分析
  6. 培训班里的36岁女青年
  7. 第5章第15节:案例:创作一份大气、华丽的卷轴动画 [PowerPoint精美幻灯片实战教程]
  8. 原材料批次管理立体仓库库位管理生产领料管理生产线工序管理产成品系列号唯一码管理产品质量追溯管理
  9. python提供数字类型包括_Python 语言提供的 3 个基本数字类型是( )_学小易找答案...
  10. 源代码应该怎么理解?