Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog does not dismiss, takes twice tap to close

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.

like image 999
Sebastian Avatar asked Oct 24 '17 18:10

Sebastian


People also ask

How do I turn off AlertDialog on Android?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.

How do I dismiss an AlertDialog in flutter?

To Dismiss Dialog user needs to make use of an inbuilt class like showDialog. The dialog route created by this method is pushed to the root navigator. If the application has multiple Navigator objects. It may be necessary to call Navigator.


1 Answers

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();
}
like image 199
ישו אוהב אותך Avatar answered Sep 24 '22 18:09

ישו אוהב אותך