Delete Sharedpreferences On Button Click
I'm a beginner to android. I have passed a sharedpreferences to intent activity. I want to remove it on button click by user. How can I execute that in code? I tried different way
Solution 1:
Retrieve a SharedPreferences object for accessing preferences that are private to this activity.
SharedPreferences preferences = getSharedPreferences("YOUR PREF", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear().apply(); //remove all//OR
editor.remove("KEY").apply(); //remove by key
use with click
button.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
editor.remove("KEY").apply(); //remove by key
}
});
Solution 2:
SharedPreference.Editorpref= context.getSharedPreferences("A_PREFS_FILE", 0).edit();
pref.clear();
pref.commit();
Post a Comment for "Delete Sharedpreferences On Button Click"