Reset SharedPreferences?

ulyssessPax picture ulyssessPax · Mar 7, 2014 · Viewed 11.4k times · Source

I used the method in ShredPreferences, this way I saved my app-settind, But I have a question. Is it possible to Reset my saved settings, and come back to default value? The code that I'm using saves the changes of ImageButton's image. I would like to reset settings and restore default value after a click of a specific Reset-Button.

Thanks for everything!

private static final String Mypref= "pref";

final SharedPreferences pref = getSharedPreferences(Mypref, Context.MODE_PRIVATE);
buttonClick1.setImageResource(pref.getInt(Mypref, R.drawable.default_value));
image.setImageResource(imageResource);

SharedPreferences.Editor editor = pref.edit();
editor.putInt("Mypref", R.drawable.users_value_chosen);
editor.commit();

Answer

sergej shafarenka picture sergej shafarenka · Mar 7, 2014

You can simply remove (clear) shared preferences. Then, when you read them, just provide default values in the code.

pref.edit().clear().commit();

The next line will use R.drawable.default_value as a default value, because preferences were deleted.