Android 偏好设置未保存

发布于 2024-09-25 16:13:29 字数 1203 浏览 3 评论 0原文

当用户更新(或者我猜是安装)我的应用程序时,我试图创建一个“新增内容”弹出对话框。我有以下逻辑来检查最后安装的版本,然后将其与当前版本进行比较,然后将当前版本保存为“最后”版本(这在我的“第一个”的 onCreate() 末尾调用“ Activity):

    int lastVersion = getPreferences(MODE_PRIVATE).getInt(LAST_VERSION, 34);
    try {
        int currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;

        Log.d(getClass().getSimpleName(), "Old Version = " + lastVersion);
        Log.d(getClass().getSimpleName(), "New Version = " + currentVersion);

        if (currentVersion != lastVersion) {
            // write the current version to the preferences so they won't see the popup again
            getPreferences(MODE_PRIVATE)
                .edit()
                .putInt(LAST_VERSION, lastVersion)
                .commit();

            // tell the user the new thing
            AlertDialog dia = new AlertDialog.Builder(this)
                .setTitle(R.string.main_menu_new_title)
                .setMessage(R.string.main_menu_new_body)
                .create();
            dia.show();
        }
    } catch (NameNotFoundException ex) {}        

问题是,无论我运行应用程序多少次,我总是会得到 lastVersion 的默认值,因此总是会出现弹出窗口。有想法吗?

I'm trying to create a "what's new" pop-up dialog when users update (or, I guess, install) my app. I have the following bit of logic to check that the last installed version was then compare it to the current version, then save the current version as the "last" version (this is called at the end of the onCreate() of my "first" Activity):

    int lastVersion = getPreferences(MODE_PRIVATE).getInt(LAST_VERSION, 34);
    try {
        int currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;

        Log.d(getClass().getSimpleName(), "Old Version = " + lastVersion);
        Log.d(getClass().getSimpleName(), "New Version = " + currentVersion);

        if (currentVersion != lastVersion) {
            // write the current version to the preferences so they won't see the popup again
            getPreferences(MODE_PRIVATE)
                .edit()
                .putInt(LAST_VERSION, lastVersion)
                .commit();

            // tell the user the new thing
            AlertDialog dia = new AlertDialog.Builder(this)
                .setTitle(R.string.main_menu_new_title)
                .setMessage(R.string.main_menu_new_body)
                .create();
            dia.show();
        }
    } catch (NameNotFoundException ex) {}        

The problem is that no matter how many times I run the app I always get the default value for lastVersion, so the pop-up ALWAYS happens. Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2024-10-02 16:13:29

您正在将版本号写回首选项。

更改:

getPreferences(MODE_PRIVATE)
                .edit()
                .putInt(LAST_VERSION, lastVersion)
                .commit();

至:

getPreferences(MODE_PRIVATE)
                .edit()
                .putInt(LAST_VERSION, currentVersion)
                .commit();

You're writing the old version number back to the preferences.

Change:

getPreferences(MODE_PRIVATE)
                .edit()
                .putInt(LAST_VERSION, lastVersion)
                .commit();

to:

getPreferences(MODE_PRIVATE)
                .edit()
                .putInt(LAST_VERSION, currentVersion)
                .commit();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文