0

Dear Friends I want to clear my shared preference I have tried this

     SharedPreferences pref = getActivity().getSharedPreferences(
             "mypassword", getActivity().MODE_PRIVATE);
     Editor editor = pref.edit();

     editor.putString("key_username", "");  
     editor.putString("key_password", ""); 
     editor.commit(); 

but its not working.How can I clear please help me.

user3519555
  • 699
  • 1
  • 5
  • 5
  • Refer [this link][1] for the answer [1]: http://stackoverflow.com/questions/24772291/how-to-save-json-from-url-and-update-the-saved-file-from-url-after-fixed-interva/24772552#24772552 – Paritosh Aug 22 '14 at 06:14

5 Answers5

2

To clear the shared preference you need to add this code.

SharedPreferences.Editor.clear();
SharedPreferences.Editor.commit();
Kaushik
  • 6,150
  • 5
  • 39
  • 54
Robin Royal
  • 1,788
  • 1
  • 18
  • 29
1

have you tried using

editor.apply();

in place of

editor.commit();
Michael Shrestha
  • 2,547
  • 19
  • 30
1

Try the following code. It will clear your SharedPreferences.

     SharedPreferences pref = getActivity().getSharedPreferences(
             "mypassword", getActivity().MODE_PRIVATE);
     Editor editor = pref.edit();
     editor.clear(); 
     editor.commit();
1

This should be used -

SharedPreferences settings = view.getContext().getSharedPreferences("mypassword", 0);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
sjain
  • 23,126
  • 28
  • 107
  • 185
0

Try:

Editor e = pref.edit();
e.remove("key_username");
e.remove("key_password");
e.commit();
kgandroid
  • 5,507
  • 5
  • 39
  • 69