关于onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

转载自http://www.cnblogs.com/droi/p/5487869.html

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

      

猜你喜欢

转载自blog.csdn.net/Li_peipei/article/details/79760635