我使用 SharedPreference 在用户登录后保存用户名,并在用户注销时清除它。
当我启动应用程序时,我将检查该值,如果该值为null,则需要登录才能使用。
然而,在android 6.0上进行检查时:
Login (save username - Toast show not null) > Logout (remove username - Toast show null) > Remove App > Reinstall app > StartApp (not login - Toast show not null, this value is user that used => issue)
这个问题不会出现在 Android < 6.0 上。
以下是我的代码:
在应用程序中初始化 sharedpreference:
public static SharedPreferences getPref() {
return mInstance.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
请检查登录:
if (App.getPref().getString("username", null) == null){
//Login
}
登录成功后保存:
App.getPref().edit()
.putString("username", s.getLoggedInUser().getUsername())
.putString("profilePicUrl", s.getLoggedInUser().getProfilePicUrl())
.putString("pk", s.getLoggedInUser().getPk())
.apply();
退出时清除:
App.getPref().edit().clear().apply();