SharedPreferences在用户卸载应用程序时未被删除。

58

有人在 Nexus 6P 设备上遇到这个问题吗?我只在运行 Google Fi 的 Nexus 6P 上遇到了这个问题。

当我安装应用程序时,在 SharedPreferences 中有一个名为userIsLoggedIn的密钥。

这个代码块:

boolean userIsLoggedIn  = SharedPrefs.userIsLoggedIn(this);

// Then in another class...

 public static boolean userIsLoggedIn(Context context) {
    // For users updating apps, if the previous key-value is a string, convert it to boolean
    try {
        return context.getSharedPreferences(LOGIN_FILE, Context.MODE_PRIVATE)
                .getBoolean(USER_LOGGED_IN, false);
    } catch (ClassCastException e) {
        Logger.e(TAG, e.getMessage());
        context.getSharedPreferences(LOGIN, Context.MODE_PRIVATE)
                .edit()
                .putBoolean(USER_LOGGED_IN, false)
                .commit();
        return context.getSharedPreferences(LOGIN, Context.MODE_PRIVATE)
                .getBoolean(USER_LOGGED_IN, false);
    }
}

现在在新的卸载操作中,这应该返回 false,但我在全新安装上进行调试时,在应用启动时得到以下结果。

enter image description here

我也在运行 Proguard,如果有影响的话,当在没有启用 Proguard 的 APK 上运行设备时,它可以正常运行。在其他设备上运行 Proguard 也是正常的。


你有尝试使用其他设备吗?代码看起来没问题。 - Chintan Rathod
我认为登录在代码中的某个地方是正确的,因为共享首选项不会表现出这样的行为,请先检查一下。 - KDeogharkar
这绝对不是问题,这段代码是首先运行的。已在其他8个设备上确认过了。 - AndyRoid
3个回答

96

21
在 Android M 及以上版本中,应用程序备份会保存在 Google Driver 中。您可以通过以下步骤禁用此功能:转到项目的清单文件,在应用程序部分下设置android:allowBackup="true"为 false,然后保存即可。

17

您可以添加到清单中:

        android:fullBackupContent="false"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接