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.
You can simply use clear() function with your variable it will clear all the shared preferences. SharedPreferences preferences = await SharedPreferences. getInstance(); await preferences. clear();
To clear all the values in the shared preferences file, call the clear() method on the shared preferences editor and apply the changes. SharedPreferences.
You use remove() to remove specific preferences, you use clear() to remove them all.
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.
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
if (settings.contains("mykey")) {
SharedPreferences.Editor editor = settings.edit();
editor.remove("mykey");
editor.apply();
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With