What will happen to the SharedPreferences on update an Android App?

Cristian here says: your Application data will be remain when user install updates.

But it must be with the same package name to detect as an update of previous App.

EboMike in Warning Android user that app update could lead to losing data from old app version? says:

Quite frankly, losing data due to an upgrade is unacceptable.

Edit:

Normally, the SharedPreferences(as well as other user data) will be kept during the update process, but sometimes, due to some “unknown” problem, the data may get lost, and I guess it is out of your control. So, you can simply believe that the SharedPreferences will be kept(see here).

So,if you want to avoid clearing user’s data in upgrading progress,you have to save main data in external storage(this can be a removable storage media,such as an SD card or an internal,non-removable, storage.) and not private for your App.Or at least put away for user to backup data before upgrading .Then in first run of your (upgraded) App,check that is there any backup file in external storage or no.

If you like to know What things must/can happen on upgrading an App?,I did not any good description for this.It is complicated and relative with Android Security,Application signing,copy protection and other topics.I mean that if you change state of your App in any above fields,it causes different result.
For example if you CHANGED COPY PROTECTION FROM ON to OFF OR OFF to ON,your App will be updated but causes all your shared preferences to get lost,file access be impossible and … .
You although have to be care in about conditions cause your new App being considered as an update for previous App(see Things That Cannot Change).

Also you have to be care in about your code,it may be caused deleting data of your databases(see update app with preloaded SQLite).

But ultimately, if be careful,you can say:

The update process only replaces the apk file(and so what is in it for
example drawables,…) and does not alter databases,sharedpreferences
and any other files that generated in run time(probably in this
case,new App is installed with the UID that is equal to UID of
previous App).

You can see these pages for more details:

Help!? Updating our applicatoin on the market deletes the saved SharedPreferences.
Market copy protection totally breaks file access after updating
Can someone explain the App update process?

Leave a Comment