Reputation: 53
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.
Upvotes: 5
Views: 14208
Reputation: 3793
1st method
Your_sharedprefrence_name..clear();
Your_sharedprefrence_name.commit();
2nd Method
Your_sharedprefrence_name.clear().commit();
3rd Method(When u want to clear the arraylist of sharedprefrence put it in loop)
Your_sharedprefrence_name.remove(String.valueOf(i)).clear().commit();
Upvotes: 0
Reputation: 3872
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
if (settings.contains("mykey")) {
SharedPreferences.Editor editor = settings.edit();
editor.remove("mykey");
editor.apply();
}
Upvotes: 16
Reputation: 7207
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();
Upvotes: 4
Reputation: 1893
You have to use remove method which is simple and described here. The only parameter is the Key you have used to save this preference.
Upvotes: 2