Sudhanshu Gaur
Sudhanshu Gaur

Reputation: 7664

How to update Shared Prefrences data when user updates the app?

I was just wondering that as suppose I am storing something in android app e.g - in Shared Preferences and suppose current version of app is n and in the updated version (n + 1), I changed the data type of my shared preference object from List<String> to List<Object>, and in the next updated version which is (n+2) I have updated the data type of object to List<Double> now when user will go to play store update his/her app, then the previous data will be there so I would have to write some code by myself that if previous version is n and current version is e.g - (n+2) now I will change the value in the Shared Prefrence in the code accordingly by writing the if-else to check what is the older version.

Is above the approach you developers use when you update any android app on the playstore?

Also Is there any possiblity like for example user has (n+2) version of my app currently installed and now user wish to download any older version of my app e.g version n, now Is there any possibility that user will be able to install this version n app while also having data restored for version (n+2), because if that will happen then as in my version n code, there will be no if-else to change data type of object from List<double> to List<String> ?

I am a newbie, have never put any app on playstore but going to, that's why asking.

Thanks

Upvotes: 1

Views: 335

Answers (2)

Unknown
Unknown

Reputation: 56

  1. Don't worry about the downgrade scenario, since Google Play doesn't support it. And even if the user manually tries to downgrade (i.e you sharing the user the .apk directly), the shared preferences will be cleared out anyway.

  2. There is no migration logic for shared preferences, like the way we have for Databases. So this has to be done manually.

  3. My suggestion is to use a different key instead of going through the if else. Do this always, since I prefer keeping my code clean, rather than the shared preferences file.

Upvotes: 1

TheWanderer
TheWanderer

Reputation: 17824

If I'm completely changing the type of Object that belongs to a certain preference key, and I'm going to publish that to production, I'm going to just change the key I use to avoid the hassle of updating it conditionally. This really isn't something that should happen at all though. If you're changing things that drastically, users aren't going to be happy any way you handle it.

To answer your second question, you can't downgrade Android apps, ie go from versionCode 3 to versionCode 2, directly. The only way to do this is to first uninstall the current version, thus clearing any SharedPreferences anyway, and install the older version. If you're using the backup feature Google gives you, the preferences might be restored, which brings me back to my first point: if you're going to change it that much, just change the key you're using.

(Google Play doesn't let users downgrade for the reason I described. It will serve users the latest version that is compatible with their device.)

Upvotes: 1

Related Questions