Android官方文档翻译 十八 4.2Pausing and Resuming an Activity

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值