当前位置: 代码迷 >> 移动开发 >> Managing the Activity Lifecycle-Pausing and Resuming an Activity
  详细解决方案

Managing the Activity Lifecycle-Pausing and Resuming an Activity

热度:10263   发布时间:2013-02-26 00:00:00.0
Managing the Activity Lifecycle---Pausing and Resuming an Activity

原文地址:https://developer.android.com/training/basics/activity-lifecycle/pausing

---------------------------------------------------------------------------------------------------------------------

During normal app use, the foreground activity is sometimes obstructed by other visual components that cause the activity topause. 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.

在通常的app使用中,前台的activity有时会被另一个可见的部件阻塞从而引起该activity发生暂停。例如,当一个半透明的activity打开时(比如某种类型的对话框),那么之前的activity就暂停了。

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 yourActivity, 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 theonResume() method.

当你的activty进入暂停阶段,系统在你的Activity中调用onPause()方法,这允许你停止正在进行的动作,而这些动作不应该在暂停时继续进行(例如一个视频),或是在用户离开你的app时保留任何应该永久保存的信息。如果用户从暂停状态返回到你的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 callsonPause() and the activity waits in the Paused state (1). If the user returns to the activity while it's still paused, the system callsonResume() (2).

图1.当一个半透明的acti阻塞你的activity时,系统调用onPause(),然后activity将在Paused状态等候(1)。如果在它暂停时,用户返回到activity,系统将调用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 theonPause() 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.

当系统为你的activity调用onPause()时,在技术上意味着你的activity仍是部分可见的,但更多的情况下这意味着用户正在离开activity,并且将到达Stopped状态。通常情况下,你应该用onPause()回调函数来:

  • 停止动画或其他正在进行的会消耗CPU的动作。
  • 提交为保存的变化,但是只在当他们离开并且期望这些变化被永久保存时(例如邮件草稿)。
  • 释放用户不需要的系统资源,例如广播收音机,传感器手柄(像GPS),或是任何可能影响电池寿命的资源。

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

例如,如果你的应用使用CameraonPause()方法是一个释放它的好地方。

@Overridepublic 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.

你应该保持onPause()中完成的操作的数目相对简单,以便当你的activity真的需要停止时,可以允许快速地向用户的下一个目标转换。

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继续时被重新调用。你不需要重新初始化那些已经在任何回调函数中创建的部件。

 

Resume Your Activity —— 恢复你的活动


 

When the user resumes your activity from the Paused state, the system calls theonResume() 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 implementonResume() to initialize components that you release duringonPause() 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()释放的部件,并且执行那些在进入Resumed状态时必须出现的初始化动作(例如开始动画,初始化那些当activity获得用户聚焦时需要的部件)。

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

下面onResume()的例子与上面的onPause()例子十分相似,因此它初始化在activity暂停时释放的照相机。

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

 

  相关解决方案