0

Is there any way to delete or reset shared preferences data when re-installing an app, without uninstalling the app and installing it?

What I mean to say is, currently, I am developing an app which uses shared preferences, but as I am still developing it, I keep running and uploading the app to the test phone through Eclipse after I make changes. Currently I am unable to run the app from the very beginning of its expected process ( after the first time) , without uninstalling the older version and then uploading the app again.

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • yes u can clear ur shared pref – KOTIOS Oct 07 '13 at 05:41
  • Could you please tell me how I could programmatically clear it when I reinstall the app? – SoulRayder Oct 07 '13 at 05:43
  • u cannot reinstall application but clear all the data and redirect user to home page – KOTIOS Oct 07 '13 at 05:47
  • Just open an adb shell and use that to clear preferences for your app. Keep the shell open while you develop and that way it can be as simple as hitting up and enter each time you want to clear shared preferences. In short you just have to clear or delete this file `/data/data//shared_prefs`. – Ali Oct 07 '13 at 06:07
  • @user2786452: Have you checked these http://stackoverflow.com/questions/3687315/deleting-shared-preferences, http://stackoverflow.com/questions/6125296/delete-sharedpreferences-file and http://stackoverflow.com/questions/6694562/how-to-reset-all-stored-data-store-using-shared-preferences – Girish Nair Oct 07 '13 at 06:10

3 Answers3

1

For this in your launching activity onCreate() method check whether the shared preference file exist or not if it is exist delete it.and later create it where ever you want.. you can check the preference file exist or not like this..

public boolean isFirstTime() {
        return getDatabasePath("your file name").exists();
    }
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • This would not be recommended as it assumes that preferences files are stored at a hard coded path which may not be true in future versions of Android. – Kuffs Oct 07 '13 at 05:51
  • But android people not yet released any version which stores the preference files in a hard coded place.. – kalyan pvs Oct 07 '13 at 05:55
  • I believe that is what I said. Using hard coded paths is bad practice and should not be encouraged. – Kuffs Oct 07 '13 at 05:58
  • without hard code you can also do like that.i can agree with you hard coding is not encouraged.. – kalyan pvs Oct 07 '13 at 06:06
1

Clear the activities like :

Intent intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Clear ur shared Pref :

SharedPreferences pref = this.getSharedPreferences("mypref", Context.MODE_PRIVATE);
getSharedPreferences("pref", Context.MODE_PRIVATE).edit().clear().commit();
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
0

Check in your launch activity whether to clear the preferences or not with a function like this:

    SharedPreferences prefs = this.getSharedPreferences("prefs", Context.MODE_PRIVATE);
    if (!prefs.getBoolean("FirstRun", true)) {
        // Not the first time so clear prefs
        prefs.edit().clear().commit();
    } else {
        // Set the value for future runs
        prefs.edit().putBoolean("FirstRun", false).commit();
    }
Kuffs
  • 35,581
  • 10
  • 79
  • 92