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

1

14

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?

Merely answered 4/11, 2016 at 13:50 Comment(4)
Possible duplicate of when i click on button to showing next activity it shows only white screen you can try the answer hereLowry
I have studied this document but unfortunately I didn't get the point: developer.android.com/reference/android/…Merely
The point is that you can save the activity state between device restarts by putting stuff in a special bundle.Mcmath
Then why we need another method implemented? Why android doesn't use the saved state even after rebooting?Merely
L
9

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

Litalitany answered 26/11, 2019 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.