I am just having a little problem on the clearing Sharing Preferences data.
protected void onStop(){
super.onStop();
SharedPreferences settings = getSharedPreferences("SharedP", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("inputValue", et1.getText().toString());
editor.commit();
}
private void exitOptionsDialog() {
new AlertDialog.Builder(this)
.setTitle("Exit")
.setMessage("Are you sure you want to exit?")
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) { } })
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
SharedPreferences settings = getSharedPreferences("SharedP", 0);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
finish();
It is a exit button saying “Yes” this button will exit the app and also clear the data entered/saved before.
I test the app and I type the value in to the textview box, press home and then back to the app the value is there and is all good and working. So I go to my menu -> exit -> dialog pop up ask the user exit or not, and Yes, the app simply close it but however when I run the app again the value is still in the textview.
Not sure what I’ve done wrong 🙁
PS: I have just updated the editor.remove(“SharedP”); with editor.clear(); and problem still remain.
You can use
editor.clear();SharedPreferences.Editor clear()