What is the usage of onCreate method second implem

2020-07-07 11:04发布

问题:

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