Search code examples
androidsharedpreferencespreferences

How to clear old preferences when updating Android app?


I have an app on the Google Play market. For various reasons that I won't bother going into, I have changed the type of some of my preferences. For example a preference type was an Integer and in the most recent version it is now a String. I imagine this isn't good practice but unfortunately I had to do it.

My concern is that when someone updates to the new version their app will crash as the preference types have changed. For this reason I would like to clear the preferences whenever the app is updated (again I realise not ideal!)

Is this possible?


Solution

  • 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
            }
        }
    }