Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset SharedPreferences?

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();
like image 455
ulyssessPax Avatar asked Mar 07 '14 13:03

ulyssessPax


People also ask

How do I clear all SharedPreferences in flutter?

simply use clear() function with your variable it will clear all the shared preferences. SharedPreferences preferences = await SharedPreferences. getInstance(); await preferences. clear();

What does the SharedPreferences editor Clear () method do?

clear(), It delete only values (means that keys are available) or it delete key value pair. check the following code. SharedPreferences userlogindetails = getSharedPreferences("userdetails", MODE_PRIVATE); SharedPreferences.

Does SharedPreferences persist after uninstall?

I was storing a String value in SharedPreferences that was setting the value for a Dropdownmenu . Now I have updated those values so the previous are no longer valid. However, even after uninstalling the app and clearing the data the old shared preference value is still there.


1 Answers

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.

like image 199
sergej shafarenka Avatar answered Sep 24 '22 09:09

sergej shafarenka