1

I want to add an disclaimer to my app which pops up at the first start of the app. If the User declines, the app closes. If the user opens the app again the disclaimer should pop up again until the user accepts it. Once accepted it should hide and don't pop up anymore.

My problem: If I accepted the disclaimer it closes and everything is fine. But when I launch the app it pops up again.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final SharedPreferences pref = getSharedPreferences("Preferences", MODE_PRIVATE);

    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)
            .setIcon(R.drawable.caution)
            .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                SharedPreferences.Editor edit = pref.edit();
                public void onClick (DialogInterface dialog, int id) { 

                    boolean accepted = true;
                    dialog.cancel();
                    if(accepted == true)
                    {
                    edit.putString("Version", this.getString(R.string.version));
                    edit.commit();}
                }
                private String getString(int version) {
                    // I had to create this method, cause i got an error the line above this.getstring(R.string.version)
                    return null;
                }
            })
            .setNegativeButton("Decline", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    MainActivity.this.finish();
                }
            });
        AlertDialog disc = builder.create();
        disc.show();
 } }

I found a quite similar question, but I couldn't solve my problem with it.

I would be really happy about an answer and if possible a good explanation in the code. Cause I wanna learn more/why it solved my problem and don't want to copy & insert a code and be happy that it works.

Community
  • 1
  • 1
Kroenig
  • 644
  • 7
  • 16

3 Answers3

4

Your problem is the if condition ver != lver, because you should check Strings for equality with equals.

if(ver.equals(lver))

Because this problem has been discussed many of times here is a link which should explain it well.

Furthermore change this part (I commentated in code)

    // this doesn't belong here. get it in onClick of the positive Button
    final SharedPreferences pref = getSharedPreferences("Preferences", MODE_PRIVATE);
    builder.setTitle("Disclaimer")
        .setMessage(this.getString(R.string.disclaimer))
        .setCancelable(false)
        .setIcon(R.drawable.caution)
        .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
            SharedPreferences.Editor edit = pref.edit(); // the same as with the pef object
            public void onClick (DialogInterface dialog, int id) { 

                boolean accepted = true; // no need for this because the if clause will always be true
                dialog.cancel(); // cancel the dialog IF the job here is done
                if(accepted == true)
                {
                edit.putString("Version", this.getString(R.string.version)); // delete the this pointer
                edit.commit();}
            }
            // no need for this method
            private String getString(int version) {
                // I had to create this method, cause i got an error the line above this.getstring(R.string.version)
                return null;
            }
        });
        .setNegativeButton("Decline", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                MainActivity.this.finish();
            }
        });

to this

    builder.setTitle("Disclaimer")
        .setMessage(this.getString(R.string.disclaimer))
        .setCancelable(false)
        .setIcon(R.drawable.caution)
        .setPositiveButton("Accept", new DialogInterface.OnClickListener() {

            public void onClick (DialogInterface dialog, int id) { 
                SharedPreferences pref = getSharedPreferences("Preferences", MODE_PRIVATE);
                SharedPreferences.Editor edit = pref.edit();
                edit.putString("Version", getString(R.string.version));
                edit.commit();
                dialog.cancel();
            }

        });
        .setNegativeButton("Decline", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                MainActivity.this.finish();
            }
        });

Now it should be fine

Community
  • 1
  • 1
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • Thanks for the comments! It worked but I used 'if(!ver.equals(lver)){...}' like @sddamico instead of 'if(ver.equals(lver)){...}'. I edit the working code to my Question – Kroenig Sep 11 '13 at 19:01
2

It looks like you're comparing two String's with a != operator. The proper way to compare strings in Java is using the .equals(String string) function.

Your comparison should probably look like this:

if(!ver.equals(lver)){
    ...
}
sddamico
  • 2,130
  • 16
  • 13
0

**Thank you All for Answers... but As per my concern Disclaimer dialog should be look like enter image description here

 i have customise the Dialog and used the same logic of SharedPrefence to save the users choice.
    The source code are ---
   ****MainActivity.java ***** 
    public class MainActivity extends Activity {
        SharedPreferences   prefs ;

        boolean isShowAgain=true;
        boolean accepted=false;
        CheckBox dialogcb;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            prefs = getSharedPreferences("detail", MODE_PRIVATE);
            boolean isshowagain =prefs.getBoolean("show_again", true);
             if(isshowagain)
                showdialog();
        }
        private void showdialog() {
            final Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.custom_dialog);
            dialog.setTitle("Disclaimer...");
            Button dialogButton = (Button) dialog.findViewById(R.id.button1);
           dialogcb= (CheckBox) dialog.findViewById(R.id.checkBox1);
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                        accepted = dialogcb.isChecked();
                        SharedPreferences.Editor edit=prefs.edit();
                        edit.putBoolean("show_again",!accepted);
                        edit.commit();
                       dialog.dismiss();
                }
            });
            dialog.show();
        }
    }*


    and the custom_dialog.xml is---
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_marginBottom="10dip"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_marginTop="10dip"
            android:text="This is alert dialog will run  every time when App will launch .
            if you want to close for always just check on check box and then press agree button" />
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Do not show it Again." />
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_marginTop="5dip"
            android:text="Agree" />
    </LinearLayout>*
nitwatar
  • 159
  • 2
  • 9