What is the usage of onCreate method second implementation in Android Activities?

巧了我就是萌 提交于 2020-01-12 18:20:52

问题


I have alway used onCreate method inside my Activity lifecycle to start or restore from a saved state, but recently found that there is another onCreate method which contains a PersistableBundle:

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);

}

I only found that it has been added since Android 21.

Could anyone please give a complete information about this method, when it calls and the usage?


回答1:


From what I've been able to gather, if you set a property on your activity in the manifest like this:

<activity 
   android:name=".MainActivity"
   android:persistableMode="persistAcrossReboots"
</activity>

then you can use the PersistableBundle to recover data after a system shutdown and restart. In other words, a normal Bundle object will keep a record of your savedInstanceState as long as the application is alive. You can use the PersistableBundle to save data in the event the system is shutdown.

You can also use persistNever or persistRootOnly instead of persistAcrossReboots.

You can find more info on the docs here: https://developer.android.com/reference/android/R.attr.html#persistableMode



来源:https://stackoverflow.com/questions/40424394/what-is-the-usage-of-oncreate-method-second-implementation-in-android-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!