简体   繁体   English

Android如何清除“共享首选项”中的数据?

[英]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。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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