8

I have been trying to learn and play with Android studio for about 3 weeks now. I just came to a situation where AlertDialogue doesn't dismiss on clicking on positive button.

private void showGPSDisabledAlertToUser() {
    AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
    } else {
        builder = new AlertDialog.Builder(this);
    }

    builder.setTitle("Turn On Location / GPS");
    builder.setCancelable(false);
    builder.setMessage("Application Needs To Determine Device's Physical Location.");
    builder.setPositiveButton("YES, TURN ON", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss(); // This ain't working
                    goToInternetSettings();
                }
            });
    builder.setNegativeButton("NO, CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    closeApplication();
                }
            });
    builder.create().show();
}

private void goToInternetSettings() {
    Intent gpsSetting = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(gpsSetting);
}

private void closeApplication() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
}

It looks like I am only able to close the dialogue if I have to double click on positive.

On the other hand, with negative button there is no such trouble. I guess, since negative button shuts down the whole application, and hence that is taking care of that problem else it would have been the same.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Sebastian
  • 286
  • 2
  • 10

2 Answers2

7

You don't need to call dialog.dismiss() inside setPositiveButton() OnClickListener because it is called implicitly.

You probably call the showGPSDisabledAlertToUser() multiple times when checking for the GPS availability. You can try to create the dialog once and reshow it again with something like this:

AlertDialog mAlertDialog;

private void showGPSDisabledAlertToUser() {
  // build the alert dialog once.
  if (mAlertDialog == null) {
    AlertDialog.Builder builder;

    ...
    // Do your dialog initialization here.
    ...

    mAlertDialog = builder.create();
  }

   mAlertDialog.show();
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • 1
    You saved my day, I call dialog multiple times @@ – Duc Trung Mai Dec 04 '20 at 09:20
  • This hint saved me. Spent hours trying to figure out why AlertDialog was behaving this way. There's no real visual transition to catch when you've closed the first one and then see the second one. Add me to the "opened the dialog twice on accident" club. – Travis Griggs Jan 12 '21 at 22:38
2

You don't need to explicitly dismiss the alert dialog by calling dialog.dismiss(); inside the onClick method of the dialog interface buttons.

The dialog will automatically be closed once you click any of those buttons.

If you're telling the dialog isn't gone after you click the button, you might have created multiple dialogs so that even one dialog is dismissed, another one is still there.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59