vikifor
vikifor

Reputation: 3466

delete shared preferences on installing new version of application in development android

I am developing Android application and I am using SharedPreferences for user credentials. My application is in development phase, so I like to delete shared preferences on installing new version of the application. Uninstalling previous version does not achive this. How can I achive this, because when I install new version of my application, the application read shared preferences like user was logged in previously. This is normal application behavior, however I would like while development, to delete shared preferences file, but only on first application start, because I also like to test if user is logged in on other application execution.

Upvotes: 2

Views: 1245

Answers (1)

Adem
Adem

Reputation: 9429

you can use versionCode in manifest. increment it when you make a new development. and then, check this to delete SharedPreference

try {
    PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    SharedPreferences pref = getSharedPreferences("NAME", 0);
    if (pref.getInt("VERSION_CODE", 0) != pInfo.versionCode) {
        Editor edit = pref.edit();
        edit.clear();
        edit.putInt("VERSION_CODE", pInfo.versionCode);
        edit.commit();
    }
} catch (NameNotFoundException e) {
    e.printStackTrace();

}

Upvotes: 2

Related Questions