码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-06-03 01:08:34      阅读:366      评论:0      收藏:0      [点我收藏+]

标签:des   android   c   style   class   code   

Pausing and Resuming an Activity

暂停和恢复活动

      

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()方法的信息,这将表明这个活动将会被暂停一会儿,然后用户才会返回到您的活动中。但是它通常第一个迹象便是用户正在离开您的活动。

bubuko.com,布布扣

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

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

标签:des   android   c   style   class   code   

原文地址:http://blog.csdn.net/xia09222826/article/details/27524373

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!