3

I am asking the same question which is asked before at below links but the solution proposed in these links is not working for me so I am posting it again.

How to make an AlertDialog disappear?

Android AlertDialog always exits when I click on OK Button

How to navigate to next activity after user clicks on AlertDialog OK button?

Basically, I am creating an AlertDialog builder to notify the user for asking to enable a setting for the Usage Data Access and when the OK button is pressed then the Settings menu gets opened. When I press back button to come back on the app then the AlertDialog is still available there although I expected to be dismissed to be back on my app.

    public void show_alert(){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("This application requires access to the Usage Stats Service. Please " +
                        "ensure that this is enabled in settings, then press the back button to continue ");
    builder.setCancelable(true);

    builder.setPositiveButton(
            "OK",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

                    Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
                    startActivity(intent);
                    dialog.dismiss();
                }
            });

    builder.show();
    return;

}

Any hint what wrong could be going here?

Pagar
  • 87
  • 1
  • 12

1 Answers1

3

Edit after some testing:

I tested OPs code on 6.0.1 and it behaved as expected - i.e. the dialog was dismissed after clicked 'OK'. I'll leave my initial answer below as an alternative that also works. Additional alternatives can be found here.


You can get a reference to your Alert Dialog it from your builder.show() method:

mMyDialog = builder.show();

In your onClick method:

mMyDialog.dismiss();

Full sample:

AlertDialog mMyDialog; // declare AlertDialog
public void show_alert(){
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setMessage("This application requires access to the Usage Stats Service. Please " +
                    "ensure that this is enabled in settings, then press the back button to continue ");
  builder.setCancelable(true);

  builder.setPositiveButton(
        "OK",
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {

                Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
                startActivity(intent);
                mMyDialog.dismiss(); // dismiss AlertDialog
            }
        });

  mMyDialog = builder.show(); // assign AlertDialog
  return;
}
Trevor Halvorson
  • 490
  • 1
  • 9
  • 20
  • Hi! Thanks for your input but the above posted code is not working for me. When I press back button from the settings page then the Dialog is still available there. I am running this code on Android 7.0 phone and I tried other suggestions posted in your link as well but no luck. – Pagar Jun 28 '17 at 17:36
  • @Pagar Is the `show_alert` method being called again after the dialog is dismissed or when you return from Settings? – Trevor Halvorson Jun 28 '17 at 23:28
  • Sorry for the confusion, it worked now, there was some synchronization problem in the calling sequence which I found on a closer look at my code. Thanks for your help! – Pagar Jun 29 '17 at 11:56