How to clear old preferences when updating Android app?
The SharedPreferences.Editor
class has a clear()
function, what removes all your stored preferences (after a commit()
). You could create a boolean flag which will indicate if updated needed:
void updatePreferences() {
SharedPreferences prefs = ...;
if(prefs.getBoolean("update_required", true)) {
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
/*....make the updates....*/
editor.putBoolean("update_required", false)
editor.commit();
}
}
And after that you need to call this in your main (first starting) activity, before you access any preferences.
EDIT:
To get the current version (The versionCode declared in the manifest):
int version = 1;
try {
version = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
if(version > ...) {
//do something
}
EDIT
If you want to do some updating operation, whenever the version changes, then you can do something like this:
void runUpdatesIfNecessary() {
int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
SharedPreferences prefs = ...;
if (prefs.getInt("lastUpdate", 0) != versionCode) {
try {
runUpdates();
// Commiting in the preferences, that the update was successful.
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("lastUpdate", versionCode);
editor.commit();
} catch(Throwable t) {
// update failed, or cancelled
}
}
}
final String PREFERENCE_NAME = "my_preference";
final String APP_VERSION_CODE = 6; /* increase this if you want to clear
preference in updated app. */
preferences = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE);
if (preferences.getInt(PREFERENCE_VERSION, 0) < APP_VERSION_CODE) {
preferences.edit().clear().apply();
preferences.edit().putInt(PREFERENCE_VERSION, APP_VERSION_CODE).apply();
}