Search code examples
androidsessionsharedpreferences

SharedPreferences not persistent


I am using a SharedPreferences in Android.Everything works great in the same session.

However once I relaunch the application, all of the preferences that were set from the previous session are lost.

Is there anything I need to specify to tell the SharedPreferences to hang around from run to run?

I am creating the preferences by calling

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

Then I set properties by e.g.

preferences.edit().putString(key, value);

and I get it by

preferences.getString(key, defaultValue);

Thanks, Victor


Solution

  • SharedPreferences are persistent accross relaunch, restart, I think the problem is you are not commiting the preferences, use following to store values in preferences:

    Preferences.Editor edit=preferences.edit();
    edit.putString(key, value);
    edit.commit();