Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 388
Mohsen Mirhoseini Avatar asked Nov 04 '16 13:11

Mohsen Mirhoseini


1 Answers

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

like image 67
Jo Momma Avatar answered Sep 22 '22 13:09

Jo Momma