2 回答

TA貢獻1811條經(jīng)驗 獲得超6個贊
這是我使用SharedPreferences.
首先創(chuàng)建一個單獨的類(我用它來保存其他信息,如 url、常量等)在其中創(chuàng)建一個SharedPreferences.
public class project_constants {
private static String PREF_NAME = "project_pref";
private static SharedPreferences getPrefs(Context context) {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public static boolean getUserLogin(Context context) {
return getPrefs(context).getBoolean("login", false);
}
public static void setUserLogin(Context context, boolean input) {
SharedPreferences.Editor editor = getPrefs(context).edit();
editor.putBoolean("login", input);
editor.apply();
}
現(xiàn)在,當用戶登錄時,您應該使用project_constants.setuserLogin(getApplicationContext,True);.
現(xiàn)在,當您要檢查用戶是否已登錄時,可以使用project_constants.getuserLogin(getApplicationContext);,如果是,則用戶已登錄,否則為否。

TA貢獻1777條經(jīng)驗 獲得超10個贊
第一次,當數(shù)據(jù)從 firebase 準備好時,您應該將數(shù)據(jù)保存在 SharedPreference 中:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
editor = pref.edit();
editor.putBoolean("userLoginCheck", false);
editor.commit();
然后您可以通過以下方式獲得下次的偏好值:
boolean isLoogenIn = pref.getBoolean("userLoginCheck", true);
添加回答
舉報