数据已缓存在 Android 6 (SharedPreferences)
Data have been cached in Android 6 (SharedPreferences)
我使用 SharedPreference 在用户 login 到我的应用程序后保存 username 并且 清除 如果用户注销。
我会在启动应用程序时检查该值,如果该值为空用户需要登录才能使用。
但是当我检查 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
不会出现此问题
这是我的代码:
在应用程序中初始化共享首选项:
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();
这是因为自动备份。在 Marshmallow 中引入。
它将备份您的数据并在重新安装时使用该备份。
如果你想关闭它从设置 -> 备份和重置 -> 自动恢复
但对于您的应用,我建议您设置共享首选项。
with key "username" to null at the time of logout
and commit.
或简单地使
android:allowBackup="false"
in your Manifest
我使用 SharedPreference 在用户 login 到我的应用程序后保存 username 并且 清除 如果用户注销。
我会在启动应用程序时检查该值,如果该值为空用户需要登录才能使用。
但是当我检查 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
不会出现此问题这是我的代码:
在应用程序中初始化共享首选项:
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();
这是因为自动备份。在 Marshmallow 中引入。
它将备份您的数据并在重新安装时使用该备份。
如果你想关闭它从设置 -> 备份和重置 -> 自动恢复
但对于您的应用,我建议您设置共享首选项。
with key "username" to null at the time of logout and commit.
或简单地使
android:allowBackup="false"
in your Manifest