title: how to make AlertDialog block the code?
body: <p>I am trying to show a messagebox with OK button. I am using AlertDialog for this purpose and I realised that it is not blocking the code. Example:</p>
  
  <pre><code>@Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
         new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 1")
          .setPositiveButton("OK", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {}
          })
          .setNegativeButton("", null)
          .show();
  
         new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 2")
         .setPositiveButton("OK", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {}
         })
         .setNegativeButton("", null)
         .show();
          //...continue with UI initialization here...
      }
  </code></pre>
  
  <p>When I start activity, it shows Alert2, When I press ok it shows Alert1 afterwards.</p>
  
  <p>I need to have blocking code dialog, so at first it should show Alert1 message, wait until user presses OK button then continue to execute the code and show Alert2 message, etc.. Example:</p>
  
  <pre><code>@Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
        msgBox("Test dlg", "Alert 1");
        msgBox("Test dlg", "Alert 2");
        //...continue with UI initialization here...
      }
  
  
  private void msgBox(String title, String msg){
  
     //?????
  
     /*  WRONG, NON-BLOCKING
     new AlertDialog.Builder(this).setTitle(title).setMessage(msg)
          .setPositiveButton("OK", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {}
          })
          .setNegativeButton("", null)
          .show();
     */
  }
  </code></pre>
  
  <p>What should I write on //????? place in msgBox method?</p>
  
  <p>MORE EXAMPLE, I need something like this:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
     if (isInitialisationDataFailed()){
        msgBox("Alert", "Cannot open activity, sorry");
        myActivity.this.finish();
        return;
      }
  }
  </code></pre>
  
  <p>But this does not work. Activity finish runs quicker than alert appear on the screen.</p>
  
  <p>The main idea to have messagebox code separate to own method to make it reusable. How to achieve this?</p>
  
  <p>/////////////////////////////////
  another example:</p>
  
  <pre><code>private void init(){
    //init code here...
    if (isSomethingWhrong()){
      msgbox("wrong stuff will be fixed");
      //do fix wrong stuff here...
    }
    if (isAnotherthingWrong()){
      msgbox("more wrong stuff will be fixed");
      //do fix more wrong stuff....
    }
    //continue init code here...
  }
  
  private void msgbox(String msg){
      //BLOCKING DIALOG REALISATION here...
  }
  </code></pre>
  
  <p>and as alternative this:</p>
  
  <pre><code>private void init(){
    //init code here...
    handleWrongStuff();
  }
  private void handleWrongStuff(){
   if (isSomethingWhrong()){
     new AlertDialog.Builder(activity)
          .setTitle("Test")
          .setMessage("wrong stuff will be fixed")
          .setPositiveButton("OK", new DialogInterface.OnClickListener() {
  
              @Override
              public void onClick(DialogInterface dialog, int which) {
                   //do fix wrong stuff here...
                   handleMoreWrongStuff();       
              }
  
          })
          .setNegativeButton("", null)
          .show();
   }
    else{
       handleMoreWrongStuff();   
    }
  }
  
  private void handleMoreWrongStuff(){
   if (isAnotherthingWrong()){
     new AlertDialog.Builder(activity)
          .setTitle("Test")
          .setMessage("more wrong stuff will be fixed")
          .setPositiveButton("OK", new DialogInterface.OnClickListener() {
  
              @Override
              public void onClick(DialogInterface dialog, int which) {
                   //do fix more wrong stuff here...    
                   continueInit();  
              }
  
          })
          .setNegativeButton("", null)
          .show();
   }
    else{
      continueInit();  
    }
  }
  
  private void continueInit(){
    //continue init code here...
  }
  </code></pre>
  
  <p>do you see the difference in complexity?  In order to make init code working in Android I need to split it to separate methods but not on logical blocks but when I need to show dialogs. Moreover the code for initialization dialogs are repeated and became ugly and unreadable.</p>