一次性AlertDialog显示在每次运行 [英] One-time AlertDialog showing on every run

查看:158
本文介绍了一次性AlertDialog显示在每次运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个免责声明弹出时,应用程序第一次运行,并且每次更新后。我做了一堆谷歌搜索和审查这里的一些问题为好,这是我的code样子:

I am trying to have a disclaimer pop up when the app is first run, and after each update. I did a bunch of googling and reviewed some questions here as well, and this is what my code looks like:

SharedPreferences pref = getSharedPreferences("Preferences", MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
String lver = pref.getString("Version", "");
String ver = this.getString(R.string.version);
if(ver != lver)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Disclaimer")
        .setMessage(this.getString(R.string.disclaimer))
        .setCancelable(false)
        .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                accepted = true;
                dialog.cancel();
            }
        })
        .setNegativeButton("Decline", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                MainMenu.this.finish();
            }
        });
    AlertDialog disc = builder.create();
    disc.show();
    if(accepted == true)
    {
        edit.putString("Version", this.getString(R.string.version));
        edit.commit();
    }
}

这code实际上是在第一次担任,但是当我改变了我的应用程序启动活动,并搬离原来的起点活动到新的这个code,它不再工作。免责声明出现在每次运行。

This code actually worked at first, but when I changed my apps starting activity, and moved this code from the original starting activity to the new one, it no longer works. The disclaimer appears on every run.

我想弹出只显示在第一次运行和更新后。我需要做什么做实现这一目标?我感到非常沮丧和由事实混淆起来,这是工作,那么wasnt。

I want the popup only to show on first run and after updates. What do I need to do to achieve this? I am very frustrated and confused by the fact that it was working, then wasnt.

推荐答案

与.equals比较字符串()是正确的方法(参见:的我怎么比较Java中的字符串?一个很好的解释),但因为我不知道如何在Android内部的工作,你说这工作之前,这是不是你的问题。你的问题是,你的支票

Comparing Strings with .equals() is the correct way (see: How do I compare strings in Java? for a good explanation) , although because I'm not sure how the android internals work and you said it worked before, that isn't your problem. Your problem is that your check

if (accepted == true) {/** code */}

未在上点击监听器运行。因为它是不是,该线程(我假设它产生一个新的线程来显示对话框)继续运行。

isn't run on the on click listener. Because it isn't, that thread (I'm assuming it spawns a new thread to show the dialog) keeps running.

我也假设你提出这code之前,你曾宣布

I'm also assuming before you moved this code, you had declared a

boolean accepted = true; //or initialized it to true somewhere

但是,当你移动它,你没有对其进行重新初始化。现在,因为原始的默认值是假的,在新的code它到达检查你之前$ PSS对话框按钮p $,从来没有提交新的版本。

But when you moved it you didn't reinitialize it. Now, because the default value of a primitive is false, in your new code it gets to the check before you press a dialog button, and never commit the new version.

我的建议是把什么是在

accepted == true

块只需到您的听众为阳性单击按钮。

block simply into your listener for the positive button click.

这篇关于一次性AlertDialog显示在每次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆