Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android官方文檔翻譯 十八 4.2Pausing and Resuming an Activity

Android官方文檔翻譯 十八 4.2Pausing and Resuming an Activity

編輯:關於Android編程

Pausing and Resuming an Activity

暫停和恢復一個activity

This lesson teaches you to

這節課教給你

Pause Your Activity

暫停你的Activity

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)。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjwvYmxvY2txdW90ZT4NCjxoMiBpZD0="pause-your-activity">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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved