Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android官方入門文檔[13]暫停和恢復一個Activity活動

Android官方入門文檔[13]暫停和恢復一個Activity活動

編輯:關於Android編程

Android官方入門文檔[13]暫停和恢復一個Activity活動


Pausing and Resuming an Activity
暫停和恢復一個Activity活動

 

This lesson teaches you to
1.Pause Your Activity
2.Resume Your Activity

You should also read
•Activities
這節課教你
1.暫停您的Activity活動
2.恢復您的Activity活動

你也應該閱讀
•Activity活動

Try it out
試試吧

Download the demo
ActivityLifecycle.zip
下載演示
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.
在正常的應用程序使用時,前台Activity活動有時通過致使所述activity暫停等可視部件阻礙。例如,當一個半透明activity打開(諸如一個在一個對話的方式),所述先前Activity活動暫停。只要Activity活動仍部分地可見的,但目前未處於焦點中的Activity活動,它保持暫停狀態。

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()方法的Activity活動,它允許你停止不應該繼續暫停時(如視頻)正在進行的行動或持續應永久保存在任何情況下,信息用戶繼續留下您的應用程序。如果用戶返回到從暫停狀態的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等待(1)。如果用戶返回到該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:
•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.
當系統調用的onPause()為您的Activity活動,這在技術上意味著你的Activity活動仍是部分可見,但大多數情況下是顯示用戶留下activity,它會很快進入停止狀態。通常你應該使用的onPause()回調至:
•停止動畫或可能消耗CPU其他正在進行的行動。
•提交未保存的更改,但前提是用戶希望這樣的改變,當他們離開(如電子郵件草稿)被永久保存。
•釋放系統資源,如廣播接收器,手柄傳感器(如GPS),或可能影響電池壽命,同時您的Activity活動將暫停,用戶不需要他們的任何資源。

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活動恢復的時候被調用。你不需要重新初始化過程中的任何的回調方法導致對續狀態中創建的組件。

 

Resume Your Activity
恢復您的Activity活動


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

When the user resumes your activity from the Paused state, the system calls the onResume() method.
當用戶從恢復暫停狀態的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活動每次進入恢復狀態任何其他初始化(如開始動畫和初始化組件僅用於而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活動暫停相機。

@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