我在这里找到了所有答案并尝试了所有解决方案,但我的共享首选项仍然没有持久性.
这是我的代码:
public static void setActivated(boolean activated) {
SharedPreferences sp = Utils.getContext().getSharedPreferences(
USER_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(ASD, activated);
editor.commit();
}
public static boolean isActivated() {
SharedPreferences sp = Utils.getContext().getSharedPreferences(USER_PREFS, Context.MODE_PRIVATE);
return sp.getBoolean(ASD, true);
}
Run Code Online (Sandbox Code Playgroud)
我也试过了:
editor.clear();
editor.put ..
editor.commit();
Run Code Online (Sandbox Code Playgroud)
我也试过了
editor.apply();
Run Code Online (Sandbox Code Playgroud)
我甚至尝试了.apply()和.commit()并没有运气.
另一个想法是尝试使用不同的模式:
...getSharedPreferences(USER_PREFS, Context.MODE_MULTI_PROCESS);
Run Code Online (Sandbox Code Playgroud)
问题是保存的值不是持久的.如果我关闭应用程序然后重新打开它,则值全部错误.
有没有人有任何想法?我还要提到问题只出现在某些设备上,例如HTC One S,三星Galaxy S3(我在不同的S3上测试过,它运行得很好).
编辑:我在按钮单击监听器上调用保存,并在加载片段时调用isActivated(在onViewCreated()之后).
谢谢!