title: Which button was clicked in a confirmation dialog
body: <p>I've a problem to get the "result" of a custom confirmation dialog.<br>
  I built the following class:</p>
  
  <pre><code>public class MessageDialog {
    private static int result;
  
    public static final int OK = 0;
    public static final int CANCEL = 1;
  
    public static int showConfirmDialog(Context context, String message) {
      result = OK;
  
      AlertDialog.Builder ad = new AlertDialog.Builder(context);
      ad.setCancelable(false);
      ad.setMessage(message);
      ad.setTitle("Confirm");
      ad.setPositiveButton("Yes", new ButtonPressedListener());
      ad.setNegativeButton("No", new ButtonPressedListener());
      ad.create().show();
  
      return result;
    }
  
    private static class ButtonPressedListener implements DialogInterface.OnClickListener {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_POSITIVE) {
          result = OK;
        } else {
          result = CANCEL;
        }
        dialog.dismiss();
      }
    }
  }
  </code></pre>
  
  <p>And call it from the MainActivity:</p>
  
  <pre><code>if (MessageDialog.showConfirmDialog(this, "Do you want to delete?") == MessageDialog.OK) {
    // yes button was clicked
  } else {
    // no button was clicked
  }
  </code></pre>
  
  <p>But it doesn't matter which button I tap, I always get the result that the yes button was clicked.</p>