首页 > 其他 > 详细

关于onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

时间:2016-05-13 14:20:57      阅读:269      评论:0      收藏:0      [点我收藏+]

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)

关于onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

原文:http://www.cnblogs.com/droi/p/5487869.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!