title: Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style)
body: <p>Coming from the .NET-environment, I'm now looking to understand how Dialogs work in Android.</p>
  
  <p>In .NET, when calling <code>MessageBox.Show(...)</code> that creates and shows a popup dialog. In the call to Show I can specify what buttons should be available in the popup, for example:</p>
  
  <pre><code>DialogResult myDialogResult = MessageBox.Show("My text here", "My caption here", MessageBoxButtons.YesNoCancel);
  </code></pre>
  
  <p>As you can see, the call to Show returns a DialogResult when a button is pressed in the popup, informing me what button was clicked. Note that in .NET, execution is halted at the line where the call to <code>Show(...)</code> is made, so it can return the value when a button is pressed.</p>
  
  <p>If I in the above example press "No" the myDialogResult will be equal to </p>
  
  <pre><code>myDialogResult == DialogResult.No
  </code></pre>
  
  <p>Since I find the .NET-way of using/creating popups very easy and intuitive, I would like that way of creating popups in Android too. </p>
  
  <p>So, the question is if anyone know how to "halt execution" like with the <code>MessageBox.Show</code>, and then return a value whenever the Button is pressed (and the dialog goes away)?</p>
  
  <h2>Edit 1</h2>
  
  <p>To be a little bit more clear:</p>
  
  <p>I need for the execution to halt and wait until the user has chosen a button to click in the popup. The code that follow the call to show the Dialog is dependent on what button is clicked in the Dialog.</p>
  
  <p>That's why I cannot use what Erich and Alex suggest, since writing code in the onClick-methods as suggested below is not going to work. The reason is that I cannot continue the "normal execution". Let me take an example:</p>
  
  <p>Let me take an example:</p>
  
  <pre><code>int nextStep = 0; // this variable will not be reached from within the onClick-methods
  
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setMessage("Hello!")
         .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                  nextStep = 1; // *** COMPILER ERROR!! ***
              }
          })
          .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                  nextStep = 2; // *** COMPILER ERROR!! ***
              }
          })
          .create().show();
  
  if (nextStep == 1)
  {
      // then do some damage
  }
  else if (nextStep == 2
      // dont do damage
  </code></pre>
  
  <p>If I wanted the execution to be dependent on the choice in the popup, I would somehow have to make all the variables in the "normal execution" (in this case <code>nextStep</code>) available in the onClick-methods, and that sounds like hell to me.</p>
  
  <h2>Edit 2</h2>
  
  <p>Another obvious example would be a popup asking <i>"Do you want to continue"</i> with the options <i>"Yes"</i> and <i>"No"</i>.</p>
  
  <p>If the user presses "Yes", the whole method should be aborted otherwise it should continue execution. How do you solve that nicely?</p>