Panjeet
Panjeet

Reputation: 157

Never show dialog again after clicking 'agree' and close app after clicking 'disagree'

In my app I show this dialog on startup. After clicking 'agree', I never want to show the dialog again. After clicking 'disagree', I want to close the app + show the dialog again when the app is relaunched. How can this be done programmatically?

This is my current code in Java, but I want it to be in Kotlin by the way.

    new AlertDialog.Builder(this)
            .setTitle("Terms of service")
            .setMessage("This app will collect information to personalize ads.")
            .setPositiveButton("AGREE", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // never show dialog again
                }
            })
            .setNegativeButton("DISAGREE", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // close app, and show dialog when the app is opened again
                }
            })
            .setIcon(R.drawable.ic_action_name)
            .setCancelable(false)
            .show();

Upvotes: 0

Views: 343

Answers (2)

Taki
Taki

Reputation: 3730

  • You need sharedpreferees to save a boolean value that would check if the user agreed or not , here is a sample of code on how to do it
SharedPreferences sharedPreferences = getSharedPreferences("prefs",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        
        new AlertDialog.Builder(this)
                .setTitle("Terms of service")
                .setMessage("This app will collect information to personalize ads.")
                .setPositiveButton("AGREE", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();  /// here you save a boolean value , 
                        // if the user agreed , check if true next app start and ignore the dialog
                        editor.putBoolean("agreed",true);
                        editor.apply();
                        
                    }
                })
                .setNegativeButton("DISAGREE", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        editor.putBoolean("agreed",false);
                        editor.apply();
                        finish();  
                        
                    }
                })
                .setIcon(R.drawable.ic_action_name)
                .setCancelable(false)
                .show();
  • Here you check of the boolean value is true or false and accordingly show dialog again or ignore it
SharedPreferences sharedPreferences =  getSharedPreferences("prefs",MODE_PRIVATE);
        boolean isAgreed = sharedPreferences.getBoolean("agreed",false);
        if(isAgreed){
            /// here ingore the dialog
        }else {
            /// show the dialog again
        }

Upvotes: 1

Rob Evans
Rob Evans

Reputation: 2874

Write the value AGREE/DISAGREE to a file ./myAppName.properties.. on startup check for the presence of the file.. if its present, read in the key=value pairs. If the value of your key is AGREE do not show the dialog.. if its DISAGREE/missing, present the user with a dialog to make a selection. How you do that is up to you - Swing/Command line, etc.

Upvotes: 0

Related Questions