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

Pausing and Resuming an Activity

暂停和恢复活动

PreviousNextGet
started

This lesson teaches you to

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

You should also read

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.

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

@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 focus
    if (mCamera == null) {
        initializeCamera(); // Local method to handle camera init
    }
}

Pausing and Resuming an Activity 暂停和恢复活动,布布扣,bubuko.com

时间: 2024-10-06 15:00:25

Pausing and Resuming an Activity 暂停和恢复活动的相关文章

Android官方入门文档[13]暂停和恢复一个Activity活动

Android官方入门文档[13]暂停和恢复一个Activity活动 Pausing and Resuming an Activity暂停和恢复一个Activity活动 This lesson teaches you to1.Pause Your Activity2.Resume Your Activity You should also read?Activities 这节课教你1.暂停您的Activity活动2.恢复您的Activity活动 你也应该阅读?Activity活动 Try it

暂停和恢复Activity Android

暂停和恢复Activity(Pausing and Resuming an Activity) 在正常的应用程序使用,前台activity有时会被其他可视化组件遮挡,从而 造成activity的暂停.例如,当一个半透明的activity打开时(如在一个风格对话框),以前的activity就暂停了.只要 activity仍然是部分可见,但目前没有获得焦点,它就依然处于暂停状态. 然而,一旦activity被完全遮挡住,并且对用户不可见了,那么它就停止了 (这是下一课需要讨论的内容). 当你的act

Android学习路线(十三)Activity生命周期——停止和恢复(Pausing and Resuming )一个Activity

先占个位置,下次翻译~ :p 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 previou

Java/Android计时器(开始,暂停,恢复,停止)

由于要做暂停和恢复,这里我就没有使用Android的CountDownTimer,而是用了Java的Timer.所以,这个方法在java肯定是通用.我也外加了Android独有的Service,有些计时器需要在Activiy关闭的情况下依然在计时,回到Activity时,显示当前的计时状态. Timer 这个Java的类,具体看Java的API说明,但是要注意一点: Timer 在cancel后,需要重新new 一次. 首先要给计时器定义三个状态:准备,开始,暂停. public static

iOS开发 -文件下载(4 暂停和恢复)

iOS开发网络篇—文件下载(四·暂停和恢复) 一.Range简单说明 通过设置请求头Range可以指定每次从网路下载数据包的大小 Range示例 bytes=0-499 从0到499的头500个字节 bytes=500-999 从500到999的第二个500字节 bytes=500- 从500字节以后的所有字节 bytes=-500 最后500个字节 bytes=500-599,800-899 同时指定几个范围 Range小结 - 用于分隔 前面的数字表示起始字节数 后面的数组表示截止字节数,没

WPF控制动画开始、停止、暂停和恢复

1.闲言 好久也没更新一博客了,自己有点发懒,同时确实这几个月来也有点忙.风机监测软件,项目中,有这样一个小需求:正常风机在旋转的时候,上位机软要做一个风机的图片,让它不停地旋转,一但检测到下面风机停止了,上位机软件界面的风机图片也要跟着停止,并且风机图片的旋转速度最好是能够与真实的速度成比例关系,这样软件才更有逼格一点.就是实现这样一个效果,看下图1,左边是一个状态指示,没有做动画,只是做了一个图片的切换,效果还看得过去吧. 图1 风机旋转动画 2.动画制作 在WPF做动画前,首先超码得有3个

Quartz的任务的临时启动和暂停和恢复

Quartz的任务的临时启动和暂停和恢复 在项目中需要手动启停某些服务,那么需要有一个控制这些任务的类.由于任务是有Quartz控制的,我们只需要通过Quartz的相关的API实现相关的功能即可. Java代码   package com.easyway.app.quartz.mgr; import java.util.Date; import java.util.List; import java.util.Map; import org.quartz.JobDataMap; import o

利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复(转)

利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复 打印过程可能很长,这时候有可能需要暂停下来做一些事情,然后回来继续接着打印 打印过程中有2个线程:一个是程序运行的主线程,一个是异步调用的打印线程,需要实现的是在主线程中的控件(暂停打印按纽)来控制打印线程   的暂停和恢复. ManualResetEvent就像一个信号灯,当有信号的时候(初始化为true或者有线程调用它的set()方法)就表示所有在等待(WaitOne())的线程,你们可以 继续运行了,当没有信号的

OGEngine之暂停、恢复游戏

原来的AndEngine中需要自己停止绘制Scene,现在OGEngine有新方法,只要一步即可实现,重写BaseGameActivity的这两个方法即可 @Override        public synchronized void onResumeGame() {                super.onResumeGame();                this.getEngine().start();                //TODO resume Audio