15

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?

Mel
  • 6,214
  • 10
  • 54
  • 71

4 Answers4

20

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
        }
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
  • Is it possible to tell when the user decides to do an update from the Google Play market? – Mel Sep 13 '12 at 01:43
  • That's exactly what this code does: if the version will be the new then you'll know that the user decided to do an update – Phate Sep 29 '12 at 07:07
  • It's not a really beautyful way to do things, but you could also start storing the current version in the sharedpreferences, and when it doesn't match with the actual version of your app (shown above, how to get it), you could also do something. – Balázs Édes Sep 29 '12 at 08:12
2
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();
}
bikram
  • 7,127
  • 2
  • 51
  • 63
0

Store version code in default SharedPreferences AND store your others preferences in version specified file , when the version change you can find your old version code and you can have your old preference file and update the new version by old data

int versionCode=0,oldVersion;
    try
    {
        versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
    }
    catch (PackageManager.NameNotFoundException e)
    {}
    SharedPreferences prefs =getSharedPreferences("MyPref" + versionCode, Context.MODE_PRIVATE);
    SharedPreferences prefsDefault = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);  


    oldVersion=prefsDefault.getInt("OldVersion",0);


    if (oldVersion!=versionCode)
    {
        if (oldVersion>0)
        {
            // ToDo Update Preferences
            String oldPrefsKey="MyPref" + oldVersion;
            SharedPreferences oldPrefs =context.getSharedPreferences(oldPrefsKey, Context.MODE_PRIVATE);
            SharedPreferences.Editor pEdit=prefs.edit();
            pEdit.putBoolean("clock24",oldPrefs.getBoolean("clock24",false));
            pEdit.putBoolean("show_clock",oldPrefs.getBoolean("show_clock",true));
            pEdit.commit();
            oldPrefs.edit().clear().commit();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                deleteSharedPreferences(oldPrefsKey);
            } else {
                try {
                    File oldFile=new File(getCacheDir().getParent() + "/shared_prefs/"+oldPrefsKey+".xml");
                    oldFile.delete();
                } catch (Exception e) {

                }
            }

        }
        prefsDefault.edit().putInt("OldVersion",versionCode).commit();




    }
-1

You can also append the version number while getting SharedPreferences like this

context.getSharedPreferences("MyKey" + app_version_no, Context.MODE_PRIVATE);

So if you update the app, the version number will change and you will have new set of preferences.

abhishek
  • 1,434
  • 7
  • 39
  • 71