Pausing and Resuming an Activity

暂停和恢复一个activity

This lesson teaches you to

这节课教给你

  1. Pause Your Activity

    暂停你的Activity

  2. Resume Your Activity

    恢复你的Activity

You should also read

你还应该阅读

  • Activities

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.

在正常的应用程序使用中,前台activity有时可能会被其它的可视组件造成阻塞,以形成暂停状态。例如,当一个半透明的activity打开时(例如一个对话框样式),先前的activity会暂停。这种情况下,这个activity仍然部分可见但是此时它不能获取焦点,它会保持paused状态。

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

然而,一旦这个activity完全阻塞,不可见的时候,它就会停止(我们将在下一节课程进行讨论)。

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进入paused状态,系统会调用你的Activity中的onPause()方法,这里面允许你停止一个不应该继续的正在进行的动作(比如一个视频),或者万一用户要离开你的应用程序的时候存留一些应该被永久保存的一些信息。如果用户从paused状态返回你的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.

注意:当你的activity收到了一个去往onPause()的调用时,它有可能表示此activity将要暂停一段时间,用户稍后会返回此activity以重新获取到焦点。然而,通常第一个迹象是用户正在离开你的activity。

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

图1. 当一个当一个半透明的activity遮住了你的activity,系统会调用onPause()方法,此activity会在Paused状态进行等待(1)。当它仍然处于paused状态时,如果用户返回了这个activity,系统会调用onResume()方法(2)。

Pause Your Activity

暂停你的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:

当系统调用你的activity中的onPause()方法时,严格意义上讲,这预示着你的activity仍然是部分可见的,但是通常情况下它表示用户正在离开此activity,不久它将进入Stopped状态。一般情况下,你应该这样使用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),或者当你的activity处于paused状态时,会影响电池寿命的用户不需要的任何资源。

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

例如,如果你的应用程序使用照相机,在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密集型工作,比如向一个数据库中写数据,因为它会让到下一个activity的转换变得比较慢(你应该在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.

如果你的activity目前被停止了,你应该保持在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被暂停后,Activity实体会一直驻留在内存中,当activity恢复的时候会被重新召回。在Resumed状态之前的任何回调的方法中创建的部分,你都不需要重新初始化。

Resume Your Activity

恢复你的Activity

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

当用户从Paused状态恢复你的activity时,系统会调用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).

你要明白系统每次调用这个方法都会让你的activity进入后台,包括当它第一次被创建时。照此,你应该实现onResume()方法以初始化那些在onPause()之间被你释放了的组件,或者运行任何每次在activity进入Resumed状态时必须出现的一些其他初始化动作(比如开始动画或者初始化那些仅仅在activity有用户焦点时才有用的组件)。

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.

下面这个onResume()的例子是与上文onPause()例子相对应的一个例子,它初始化了在activity停止是被释放的camera。

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

Next: Stopping and Restarting an Activity

下一节:停止和重新启动一个Activity

这是我自己翻译的,如果您发现其中有重要错误,敬请指出,感谢!

Android官方文档翻译 十八 4.2Pausing and Resuming an Activity的更多相关文章

  1. Android官方文档翻译 十六 4.Managing the Activity Lifecycle

    Managing the Activity Lifecycle 管理activity的生命周期 Dependencies and prerequisites 依赖关系和先决条件 How to crea ...

  2. Android官方文档翻译 十五 3.3Supporting Different Platform Versions

    Supporting Different Platform Versions 支持不同的平台版本 This lesson teaches you to 这节课教给你 Specify Minimum a ...

  3. Android官方文档翻译 十四 3.2Supporting Different Screens

    Supporting Different Screens 支持不同的屏幕 This lesson teaches you to 这节课教给你 Create Different Layouts 创建不同 ...

  4. Android官方文档翻译 十二 3.Supporting Different Devices

    Supporting Different Devices 支持不同设备 Dependencies and prerequisites 依赖关系和先决条件 Android 1.6 or higher A ...

  5. Android官方文档翻译 十 2.3Styling the Action Bar

    Styling the Action Bar 设计菜单栏的样式 This lesson teaches you to 这节课教给你 Use an Android Theme 使用一个Android主题 ...

  6. Android为TV端助力 转载:Android绘图Canvas十八般武器之Shader详解及实战篇(上)

    前言 Android中绘图离不开的就是Canvas了,Canvas是一个庞大的知识体系,有Java层的,也有jni层深入到Framework.Canvas有许多的知识内容,构建了一个武器库一般,所谓十 ...

  7. Android为TV端助力 转载:Android绘图Canvas十八般武器之Shader详解及实战篇(下)

    LinearGradient 线性渐变渲染器 LinearGradient中文翻译过来就是线性渐变的意思.线性渐变通俗来讲就是给起点设置一个颜色值如#faf84d,终点设置一个颜色值如#CC423C, ...

  8. android官方文档翻译(不断更新中。。。)

    最近在自学android,抽空把官方文档的guide跟training差不多看了一遍,又对比了一些书籍,感觉还是官方文档讲得比较好,所以自己计划把官方文档翻译一下,方便自己的知识巩固以及复习查找,由于 ...

  9. Android进阶(二十八)上下文菜单ContextMenu使用案例

    上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...

随机推荐

  1. pwnable_start & ciscn_2019_es_2 & ez_pz_hackover_2016 & pwn2_sctf_2016

    花了两天时间做了这四道题,感觉收获很多.但是这种收获感觉写文章写不出自己的思路,就录制了一个视频. pwnable_start 这道题考察了系统调用,shellcode的编写,和动态调试的知识. ci ...

  2. HGAME pwn ROP_LEVEL2

    花了好多天,终于把这个题彻底弄懂了...自己太菜了    下载文件,首先checksec检查一下保护. 只开启了堆栈不可执行,接下来拖到IDA看一下C的伪代码. 大致先让你输入,然后再次让你输入. 第 ...

  3. LuoguP6850 NOI 题解

    Content 小 L 参加了 \(\texttt{NOI}\),现在他告诉你九个数 \(a,b,c,d,e,f,g,h,i\),分别表示--笔试作对的题数.D1T1.D1T2.D1T3.D2T1.D ...

  4. java 多线程:线程死锁,如何判断程序是否有死锁代码块儿

    线程死锁 死锁是指两个或两个以上的线程在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,若无外力作用,它们都将无法推进下去(百度百科). 代码示例: /** * @ClassName ...

  5. 怎么从svn服务器上把工程导入到MyEclipse里

    怎么从svn服务器上把工程导入到MyEclipse里,步骤如下:

  6. C# 使用Fluent API 创建自己的DSL

    DSL(Domain Specified Language)领域专用语言是描述特定领域问题的语言,听起来很唬人,其实不是什么高深的东西.看一下下面的代码: using FlunetApiDemo; v ...

  7. FastAPI(六十五)实战开发《在线课程学习系统》基础架构的搭建

    在之前三篇,我们分享的就是需求的分析,基本接口的整理,数据库链接的配置.这次我们分享项目的基本框架,目录结构如下: common目录 通用的目录,一些通用的处理放在这里 models目录 数据库相关的 ...

  8. c++设计模式概述之备忘录

    代买写的不够规范,,目的是缩短篇幅,实际中请不要这样做. 1.概述 和这个模式相似的生活场景,比如 office的撤销操作.VS  和 xcode等IDE的撤销操作 . 其实都是恢复到上一个或者下一个 ...

  9. Harry Potter and the Hide Story(hdu3988)

    Harry Potter and the Hide Story Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 ...

  10. 在线编辑Word——插入图片、图形

    在Word中支持插入图片.图形等元素,同时支持对插入的图片和图形进行格式化操作,如裁剪.调整尺寸大小.调成颜色.阴影.倾斜角度.透明度等等.本文,将通过使用Spire.Cloud Word在线编辑器来 ...