Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to clear the value of the shared preference

I have stored the username and the password in the sharedpreference. And I am displaying the username in every activity like Welcome "Username". But at the time of logout I have put one checkbox in the dialog box.If the check box is checked the sharedpreference value should be clear. So I don't know how to do it.Please help me out of it. Thank you.

like image 210
kushal45 Avatar asked Aug 19 '11 07:08

kushal45


People also ask

How do you clear preferences in flutter?

You can 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?

To clear all the values in the shared preferences file, call the clear() method on the shared preferences editor and apply the changes. SharedPreferences.

How do you delete preferences on Android?

You use remove() to remove specific preferences, you use clear() to remove them all.

How does shared preference work?

A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.


2 Answers

SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
if (settings.contains("mykey")) {    
    SharedPreferences.Editor editor = settings.edit();
    editor.remove("mykey");
    editor.apply();    
}
like image 145
Pete Avatar answered Sep 17 '22 17:09

Pete


The method to clear the sharedpreferences is this

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#clear()

With this you dont delete the xml

Editor.clear();

Editor.commit();
like image 40
Aracem Avatar answered Sep 20 '22 17:09

Aracem