这是我的主要活动类,我可以通过共享首选项在其他类中传递和检索数据,但无法清除其他类中的共享偏好。同时告诉我如何检查我的共享首选项已被清除。无法清除碎片首选项
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();
删除_editor.apply(); _。您已经调用_editor.commit(); _ – Piyush
SharedPreferences的名称不是一个。所以不能清除碎片偏好。请先检查文件名。 – Mick
我已更改名称并删除了editor.apply(); –