简体   繁体   中英

Is savedInstanceState kept during updates?

So, Activity.onCreate() takes a Bundle as a parameter, by default it's called savedInstanceState . Is the savedInstanceState kept when the app is updated to a newer version? (let's just say through Google Play). Can I put some data in savedInstanceState , update the app to a new version, and still access the data?

The documentation states:

if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed. The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.

But there is no reference about what happens if the application is killed while updating.

You should use SharedPreferences to persist information safely.

Saved instance data is not preserved during application updates. This actually makes sense not only because of the data type potential incompatibility risk among different versions but also because your new binaries could simply be completely new application, sharing just packageId and signing certificate. That's why new version requires restart - after update user will start with fresh state as this is much safer than trying to restore the app to the state it was before update started (which can be simply impossible).

So if you want data to remain unaffected by updates, database or shared preferences is much better choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM