Jesús Gómez
Jesús Gómez

Reputation: 131

Cannot dismiss 2 AlertDialogs at once in Android

I've built an android AlertDialog (named dialog) that fires other Android AlertDialog (named dialog2) if some conditions happen.

I've checked that what if only one of them is displayed on screen it is dismissed without any problem.

Problem comes when both of them are showing, that happens when I press the OK button for the second dialog, it just closes the second dialog, despite the first dialog is even showing on screen.

This is the code related to dialog2 for that operation:

dialog2.setOnShowListener(new DialogInterface.OnShowListener() 
{

   @Override
   public void onShow(final DialogInterface dialog) 
   {

      Button button = ((AlertDialog)               
      dialog).getButton(AlertDialog.BUTTON_POSITIVE);

      button.setOnClickListener(new View.OnClickListener() 
      {

          @Override
          public void onClick(View view) 
          {

              [some more operations]
              dialog2.dismiss();
              dialog.dismiss();

          }
      });
  }

});

The strangest thing is that if I supress the line dialog2.dismiss(); by leaving only dialog.dismiss(); what get dismissed is the second dialog, not the first one, looks like android somehow confuses one with the other, and I don't think that should be happening because they are created separately like this:

dialog=[code to create that dialog]
dialog2=[code to create that dialog]

Doing that the only way I see that app would close dialog2 when asked to close dialog would be doing dialog=dialog2, which I am not. I think They should be different objects loaded in memory each one with their characteristics.

I don't see any reason of why this is happening, seems like a clueless error from my point of view. Hope you can give ideas about what is happening.

Upvotes: 0

Views: 168

Answers (1)

swerly
swerly

Reputation: 2105

A couple things to take note of here:

First, it isn't necessary to create an "onShowListener", unless you actually need to perform a task when the dialog is shown, this code should help you correctly create an AlertDialog:

new AlertDialog.Builder(getContext())
                .setTitle(R.id.dialog_title)
                .setMessage(R.id.dialog_message)
                .setPositiveButton(R.id.positive_text, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                     //do onClick stuff here   
                    }
                })
                .show();

This example does all your setup at once. If you need a reference to this dialog or don't want to show it right away, just use AlertDialog.Builder dialog1 = new ..., and then use dialog1.show() to create the dialog.

Second, the reason that only the second dialog is closing when you suppress dialog2.dismiss() is because there is a local variable named 'dialog' inside of your onShow() method (look at the method parameters) that is taking precedence over your broader scoped 'dialog' variable.

Third, to answer your actual question, can you dismiss the first dialog just before you show the second? I don't see any real reason to have 2 dialogs open at the same time.

Upvotes: 1

Related Questions