【发布时间】:2016-12-16 10:02:42
【问题描述】:
这是在我的主要活动类中,我可以通过共享首选项传递和检索其他类中的数据,但我无法清除其他类中的共享首选项。还告诉我如何检查我的共享偏好是否已清除。
SharedPreferences sharedPreferences;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "userKey";
public static final String Pass = "passKey";
sharedPreferences=getApplicationContext().getSharedPreferences(MyPREFERENCES,MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Name,userName);
editor.putString(Pass,password);
editor.commit();
这是在我的另一堂课
SharedPreferences sharedPreferences;
sharedPreferences=getApplicationContext().getSharedPreferences(SignUPActivity.MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
editor.commit();
【问题讨论】:
-
SharedPreferences 的名称不是一个。所以无法清除碎片偏好。请先检查文件名。
-
删除 editor.apply();。你已经调用了 editor.commit();
-
我已更改名称并删除了 editor.apply();
标签: android