简体   繁体   中英

Android how to clear data from Shared Preferences?

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.

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

SharedPreferences.Editor.clear();
SharedPreferences.Editor.commit();

have you tried using

editor.apply();

in place of

editor.commit();

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();

This should be used -

SharedPreferences settings = view.getContext().getSharedPreferences("mypassword", 0);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();

Try:

Editor e = pref.edit();
e.remove("key_username");
e.remove("key_password");
e.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM