【Android翻譯】關於Activity的onSaveInstanceState調用時機的說明

關於Activity的onSaveInstanceState調用時機的說明
Activity的生命週期裏並沒有提到onSaveInstanceState的觸發,這個函數提供了爲我們在某些情況下保存Activity信息的機會,但需要注意的是這個函數不是什麼時候都會被調用的,官方文檔解釋的比較清楚,特此
翻譯一下。
原文出處:android-sdk-windows-1.5_r3/docs/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

protected void onSaveInstanceState (Bundle outState)

       Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both). This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).

 

    在activity被殺掉之前調用保存每個實例的狀態,以保證該狀態可以在onCreate(Bundle)或者onRestoreInstanceState(Bundle) (傳入的Bundle參數是由onSaveInstanceState封裝好的)中恢復。這個方法在一個activity被殺死前調用,當該activity在將來某個時刻回來時可以恢復其先前狀態。例如,如果activity B啓用後位於activity A的前端,在某個時刻activity A因爲系統回收資源的問題要被殺掉,A通過onSaveInstanceState將有機會保存其用戶界面狀態,使得將來用戶返回到activity A時能通過onCreate(Bundle)或者onRestoreInstanceState(Bundle)恢復界面的狀態。

 

    Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.
  
    不要將這個方法和activity生命週期回調如onPause()或onStop()搞混淆了,onPause()在activtiy被放置到背景或者自行銷燬時總會被調用,onStop()在activity被銷燬時被調用。一個會調用onPause()和onStop(),但不觸發onSaveInstanceState的例子是當用戶從activity B返回到activity A時:沒有必要調用B的onSaveInstanceState(Bundle),此時的B實例永遠不會被恢復,因此係統會避免調用它。一個調用onPause()但不調用onSaveInstanceState的例子是當activity B啓動並處在activity A的前端:如果在B的整個生命週期裏A的用戶界面狀態都沒有被破壞的話,系統是不會調用activity A的onSaveInstanceState(Bundle)的。
 
    The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself. If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().

 

      默認的實現負責了大部分UI實例狀態(的保存),採用的方式是調用UI層上每個擁有id的view的onSaveInstanceState() ,並且保存當前獲得焦點的view的id(所有保存的狀態信息都會在默認的onRestoreInstanceState(Bundle)實現中恢復)。如果你覆寫這個方法來保存額外的沒有被各個view保存的信息,你可能想要在默認實現過程中調用或者自己保存每個視圖的所有狀態。如果被調用,這個方法會在onStop()前被觸發,但系統並不保證是否在onPause()之前或者之後觸發。

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章