我想保存一個布爾值到共享首選項的值爲true,但是當我登錄它時,我繼續看到它返回false值。請參閱下面的代碼,並且請記住,此代碼位於一個片段內。我期待我的布爾被保存在共享首選項爲真,但它總是保存爲假
SharedPreferences AppPreferences = getActivity().getSharedPreferences("myPrefs", Activity.MODE_PRIVATE);
boolean propertyManagerLoggedIn = AppPreferences.getBoolean(PROPERTYMANAGER_LOGGEDIN, false);
if(!propertyManagerLoggedIn)
{
SharedPreferences.Editor editor = AppPreferences.edit();
transitionInterface.showDashboardIcons();
AppPreferences.edit().putBoolean("PROPERTYMANAGER_LOGGEDIN", true);
editor.commit();
//boolean vlaue = prefs.getbooleanflag(context, false);
Log.d("tag",""+propertyManagerLoggedIn);
}
else
{
Log.d("tag",""+propertyManagerLoggedIn);
}
及以下的代碼從我AppPreferences每次調用edit()
新Editor
時間類
public final static String PROPERTYMANAGER_LOGGEDIN = "PROPERTYMANAGER_LOGGEDIN";
public static boolean propertyManagerLoggedn(Context context)
{
TinyDB settings = new TinyDB(context);
return settings.getBoolean(AppPreferences.PROPERTYMANAGER_LOGGEDIN);
}
謝謝,讓我試試。我也懷疑我在應用程序首選項中聲明的方式是錯誤的。什麼是正確的方式來聲明它,以便我可以訪問它 – Zidane