關於onCreate(Bundle savedInstanceState, PersistableBundle persistentState)


API 21為Activity增加了一個新的屬性,只要將其設置成persistAcrossReboots,activity就有了持久化的能力,另外需要配合一個新的bundle才行,那就是PersistableBundle。 

onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

Same as  onCreate(android.os.Bundle) but called for those activities created with the attribute  persistableMode set to  persistAcrossReboots.


這里的持久化與傳統意義的不同,它的具體實現在Activity重載的onSaveInstanceState、onRestoreInstanceState和onCreate方法。

public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

onSaveInstanceState和onRestoreInstanceState方法是一對拯救災難的方法,它們不在“正常“的Activity生命周期中,只有一些突發異常情況才會觸發它們,比如橫豎屏切換、按Home鍵等。當API 21后增加了PersistableBundle參數,令這些方法有了系統關機重啟后數據恢復的能力。

只需在Manifest中的activity設置屬性:

android:persistableMode="persistAcrossReboots"

然后在activity中直接用上述的三個方法即可。 
另外注意API版本是21及以上。

 

參考資料:http://developer.android.com/reference/android/app/Activity.html

       Android實戰技巧之二十六:persistableMode與Activity的持久化

      關於onCreate(Bundle savedInstanceState, PersistableBundle persistentState)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM