Pausing and Resuming an Activity

暂停和恢复活动

Previous Next Get started      

This lesson teaches you to

  1. Pause Your Activity               暂停活动
  2. Resume Your Activity            恢复活动

You should also read

  • Activities

Try it out

Download the demo

ActivityLifecycle.zip

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, it stops (which is discussed in the next lesson).

然而,一旦活动完全阻塞和不可见,它就会停止(这是下节课所讨论的)。

As your activity enters the paused state, the system calls the onPause() 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中调用onPause()方法,您可以停止正在进行的行为,不应该继续而是暂停下来(如视频)或这保存信息,这些信息是应永久保存,以防用户继续离开应用程序。如果用户从暂停状态重新返回到您的活动中,系统会恢复它以及调用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.

注意:当您的活动收到调用onPause()方法的信息,这将表明这个活动将会被暂停一会儿,然后用户才会返回到您的活动中。但是它通常第一个迹象便是用户正在离开您的活动。

Figure 1. When a semi-transparent activity obscures your activity, the system calls onPause() and the activity waits in the Paused state (1). If the user returns to the activity while it's still paused, the system calls onResume() (2).

图一所示,当一个半透明的活动掩盖了您的活动时,系统便会调用onPause()方法,活动便会在暂停状态(1)等待。如果用户回到活动中,当它仍然保持暂停状态时,系统将调用onResume() (2).

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:

当系统为您的活动调用onPause()方法,它意味着您的活动仍然部分可见,但是通常表明用户正在离开活动,它将在不久之后处于停止状态。您通常情况下应该使用onPause()方法进行回调:

  • Stop animations or other ongoing actions that could consume CPU.                   停止动画或其他可能消耗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.

释放系统资源,如广播接收器,处理传感器(如GPS),或任何可能会影响电池寿命,而你的活动暂停,用户不需要它们的资源,。

For example, if your application uses the Camera, the onPause() method is a good place to release it.

例如,如果您的应用程序使用Camera,onPause()方法就是一个很好地方式来释放它。

@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 within onPause() 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()).

一般来说,您不应该使用onPause()方法来存储用户变换来永久保存(表单输入个人信息),只有当您确定用户期望的变化自动保存是,您应该调用onPause()方法来促使用户变换达到永久保持。但是,您应该在onPause()方法使用中避免执行CPU密集型工作期间,例如写入数据库,因为它可以减缓可见程度到下一个活动(您应该在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.

您应该在onPause()方法使用中保持操作相对简单,以允许当您的活动停止时,快速过渡到下一个目标活动。

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.

注意:当您的活动被停止,Activity实例将一直停留在内存中,当活动被恢复是被召回。您不需要创建初始化任何在之前所使用的回调方法已达到恢复状态的组件

Resume Your Activity

恢复您的活动


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

当用户从暂停状态回复您的活动,系统会调用onResume()方法。

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).

请注意,每次您的活动进入前台以及第一次被创建的时候,系统都会调用这个方法。因此,您应该实现onResume()方法来初始化组件,这些组件是您在onPause()方法中释放的或者是在活动每次进入恢复状态必须发生的其他初始化的组件(例如开始动画和初始化组件只使用而活动拥有用户的焦点)。

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.

下面是一个有关于对应在onPause()方法之前的onResume()方法的例子,因此当活动暂停的时候它初始化是相机被释放了。

@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}
}

Pausing 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. Android学习路线(十三)Activity生命周期——暂停和恢复(Pausing and Resuming )一个Activity

    在正常使用应用的过程中,前台的activity在一些时候会被其他的组件遮挡,导致这个activity暂停.举个例子,当一个半透明的activity被打开(例如一个dialog样式的activity), ...

  3. Android官方开发文档Training系列课程中文版:管理Activity的生命周期之暂停和恢复Activity

    原文地址 : http://android.xsoftlab.net/training/basics/activity-lifecycle/pausing.html 在APP的正常使用过程中,在前台工 ...

  4. python暂停和恢复游戏,暂停/恢复中间的python脚本

    我可以在用户中间暂停正在运行的python脚本(在Windows下),并在用户决定时再次恢复吗? 有一个主管理器程序生成,加载和运行其他python脚本(通过从控制台调用python script.p ...

  5. java 停止定时器_Java/Android计时器(开始,暂停,恢复,停止)

    由于要做暂停和恢复,这里我就没有使用Android的CountDownTimer,而是用了Java的Timer.所以,这个方法在java肯定是通用.我也外加了Android独有的Service,有些计 ...

  6. java暂停计时器_Java/Android计时器(开始,暂停,恢复,停止)

    由于要做暂停和恢复,这里我就没有使用Android的CountDownTimer,而是用了Java的Timer.所以,这个方法在java肯定是通用.我也外加了Android独有的Service,有些计 ...

  7. SpringBoot集成Quartz实现定时任务的动态创建、启动、暂停、恢复、删除。

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 作者:毅大师 blog.csdn.net/qq_39648 ...

  8. Qt实用技巧:使用OpenCV库的视频播放器(支持播放器操作,如暂停、恢复、停止、时间、进度条拽托等...

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 需求 使用OpenCV库的视频播放器(支持播放器操作,如暂停.恢复 ...

  9. android如何暂停倒计时,在Android中暂停和恢复倒计时器和进度条?

    我正在制作一个简单的应用程序,它使用倒计时器,循环进度条和3个按钮启动,暂停和恢复 . 我想要做的是当特定活动开始时我按暂停它存储定时器的时间暂停并从那一点开始恢复 . 但问题是倒计时器没有停止,所以 ...

最新文章

  1. python 中文件输入输出及os模块对文件系统的操作
  2. Maven对插件进行全局设置
  3. java盒图_java合成图片
  4. 【Python成长之路】从零做网站开发 -- 下拉选择项的实现
  5. [LeetCode]题15:3Sum
  6. return语句的用法
  7. 磁盘位置_ORACLE RAC ASM磁盘组迁移到新的ASM磁盘组方法
  8. php扩展intl安装不成功,phpstudy开启php_intl扩展启动弹出错误的解决方法
  9. 区块链技术指南 序章理解感悟
  10. 飞控硬件在环之GPS模拟器
  11. html广告清理,谷歌浏览器插件-清除CSDN广告
  12. paraview视图vtkView
  13. 计算机导论第二版清华大学答案,计算机导论(第2版)课后习题答案【清华大学出版社】(13页)-原创力文档...
  14. 二手升腾网络计算机,瘦客户机终端网络计算机专用计算机
  15. 什么是G.652光缆
  16. 浅谈tomcat优化
  17. 使用IBM InfoSphere Guardium Data Redaction在保护隐私的同时共享信息
  18. 有趣且重要的JS知识合集(18)浏览器实现前端录音功能
  19. 使用灰度图作为数据集训练CNN
  20. 陕师大计算机期末考试题,陕师大物理化学(上)历年期末考试题

热门文章

  1. 真实的谎言!天河一号排名中国第一超算名不副实
  2. i5四核八线程怎么样_8代酷睿首测 4核8线程!i5 8250U移动CPU
  3. Asp.net Authenticatiion Authorization(认证与授权)——表单验证
  4. 这样的老板,真想把他拖进WC爆zou一顿.....
  5. 大数据分析技术的发展趋势
  6. trokuti 三角形
  7. 小米电视es2022款55寸 65寸 75寸参数配置
  8. 抖音创始人张一鸣:10年面试2000人,我发现混的好的人,全都有同一个特质
  9. 文件下载:POI读取word或Excel,修改内容后以流的形式输出到前端
  10. java学籍管理系统课设报告,基于SSM+Redis+LayUI的大学生学籍信息管理系统-java学生学籍信息管理系统mysql数据源...