我有一个共享首选项,并且我创建了一个方法来检查用户是否是第一次使用该应用程序。但它总是返回与默认值相反的值。
我的代码:
public static Boolean getFirstOnApp(Context context) {
SharedPreferences pref = context.getSharedPreferences(LOGIN_PREFS_NAME, Context.MODE_PRIVATE);
return pref.getBoolean(KEY_FIRST_TIME_ON_APP, true);
}
总是返回false
。
我在我的 Controller 上调用它:
if (SaveSharedPreferences.getFirstOnApp(context)) {
fabAtivaMapeamento.performClick();
SaveSharedPreferences.setFirstOnApp(context, false);
}
SaveSharedPreferences.setFirstOnApp(context, false);
以前从未被调用过。它仅在此 If
我已经卸载了该应用,强制其停止,清除了数据和缓存。
如何解决?
最佳答案
像这样使用来检查是否是第一次
SharedPreferences pref = getSharedPreferences(LOGIN_PREFS_NAME, Context.MODE_PRIVATE);
if (pref.getBoolean(KEY_FIRST_TIME_ON_APP, false)) {
//in here it's not for first time
} else {
//in here it's first time
}
关于java - SharedPreferences 不返回默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57100429/