0

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();
    }
}

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.

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.

  • 3
    Don't ever compare `Strings` in Java using `==` or `!=`. You have an `if` condition `if(ver != lver)`. Change that to use the `equals(...)` method of `String` as follows... `if(!ver.equals(lver))`. – Squonk Jul 29 '12 at 21:28
  • I made that change. Just to verify I'm understanding the syntax you gave, the ! before the first variable name makes it if not equal to? – DEF Software Solutions Jul 29 '12 at 21:37
  • And also, making that change didnt solve the issue, still comes up every run. – DEF Software Solutions Jul 29 '12 at 21:37
  • Yes `!` means `NOT` therefore `!someString.equals(someOtherString)` means "not equal to". If changing it to use `if(!ver.equals(lver))` hasn't fixed it then either there's a problem with `R.string.version` or with the `Version` preference. Are you saving that using the name `version`? Preferences are case-sensitive - if you are saving as `version` and trying to retrieve as `Version`, then the default `""` of `pref.getString("Version", "")` will be applied. – Squonk Jul 29 '12 at 21:48
  • No, both saving and retrieving as `Version`. You can see the storage at the bottom of the snippet, and the retrieval at the top. – DEF Software Solutions Jul 29 '12 at 21:53
  • OK, sorry, missed the saving part. – Squonk Jul 29 '12 at 21:57
  • Ah, OK. You need to put the saving of the version into the `onClick(...)` method of the `OnClickListener` of the `.setPositiveButton` call of the dialog builder. – Squonk Jul 29 '12 at 22:02

1 Answers1

3

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.

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

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

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.

My advice would be put what's in the

accepted == true

block simply into your listener for the positive button click.

Community
  • 1
  • 1
aamit915
  • 509
  • 4
  • 16
  • "My advice would be put what's in the `accepted == true` block simply into your listener for the positive button click." If I do this i get the error "Cannot refer to a non-final variable edit inside an inner class defined in a different method". – DEF Software Solutions Jul 29 '12 at 22:12
  • EDIT: Ok - you can either make pref a final variable (and only create the editor on the positive button being pressed as you don't use it elsewhere) or in the accept call a function you defined. So I'd just final SharedPreferences pref ... ... public void onClick (DialogInterface dialog, int id) { SharedPreferences.Editor edit = pref.edit(); //Handle your edit code. } – aamit915 Jul 29 '12 at 22:14
  • However, your second thing was correct. I had moved the boolean declare, but for some reason did it as false instead of true, therefore the version was never being stored lol. – DEF Software Solutions Jul 29 '12 at 22:14