Android中關於onSaveInstanceState文檔

來源:互聯網
上載者:User

大家在碼代碼的時候,每個activity肯定是會調用onCreate方法的,但是onCreate方法中的savedInstanceState這個參數卻很少用到,經常就這麼被忽略了,從其命名來看,應該是用來儲存activity狀態的,而這些狀態資料是從何而來呢?就是自己這回要學習的onSaveInstanceState方法,沒有什麼資料比官方文檔更準確詳細的了, onSavedInstanceState方法的翻譯,記錄學習一下該方法的使用。

   

protected void onSaveInstanceState (Bundle outState)

Added in API level 1

 

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).

 

在一個activity被殺掉之前調用來儲存每一個執行個體的狀態,這樣一來該狀態便可以在onCreate(Bundle)或者onRestoreInstanceState(Bundle) 中恢複。

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

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即將被殺掉之前調用,以便於在將來某一時刻返回時可以恢複到其原來的狀態。比如,如果activityB啟動後位於activityA的前面,並且在某一時刻activityA由於系統回收資源要被殺掉,A就可以通過該方法將有機會儲存其使用者介面的狀態,這樣,以後當使用者返回到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()在activity被放置後台或者自行銷毀時被調用,onStop()是activity被銷毀時調用。有這樣一個說明什麼時候onPause和onStop被調用而不是onSaveInstanceState被調用的例子就是當使用者從activityB返回到activityA的時候:沒有必要調用B的onSaveInstanceState(Bundle)因為此時的B的執行個體永遠不會被恢複,所以系統會避免調用它。另一個調用onPause()而不是調用onSaveInstanceState()的例子是當activityB啟動並處於activityA的前端:如果在B的生命週期裡A的介面使用者狀態未被破話,系統就不會調用activityA 的onSaveInstanceState(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.

 

預設的實現是通過調用UI層面上每個擁有ID的view的onSaveInstanceState()的方式來儲存大部分UI執行個體的狀態,並且儲存當前獲得焦點的view的id(所有的儲存狀態都會在預設的onRestoreInstanceState(Bundle)實現中恢複)。如果你重寫這個方法來儲存額外的沒有被每個單獨的view儲存的資訊,你可能想要通過在預設的實現中調用,或者自己來儲存每個view的所有的狀態。

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

If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().

 

如果被調用,該方法會在onStop()之前發生。但並不保證是否在onPause()之前或之後發生。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.