title: Cannot dismiss 2 AlertDialogs at once in Android
body: <p>I've built an android AlertDialog (named dialog) that fires other Android AlertDialog (named dialog2) if some conditions happen.</p>
  
  <p>I've checked that what if only one of them is displayed on screen it is dismissed without any problem.</p>
  
  <p>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.</p>
  
  <p>This is the code related to dialog2 for that operation:</p>
  
  <pre><code>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();
  
            }
        });
    }
  </code></pre>
  
  <p>});</p>
  
  <p>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:</p>
  
  <pre><code>dialog=[code to create that dialog]
  dialog2=[code to create that dialog]
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>