关于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)

转载于:https://www.cnblogs.com/droi/p/5487869.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值