title: Android modal dialog stacking
body: <p>My goal is to have a modal dialog display when the user navigates back to my app. This modal dialog asks them for a 6 digit PIN that they defined. I'm new to Java GUI and Android development so my problem is.....</p>
  
  <p>I find that if the user chooses to minimize the app while the enterpin dialog is showing, if they return to the app the dialogs stack on top of each other. Making them enter their PIN as many times as they minimized and returned to the app during PIN entry.</p>
  
  <pre><code>/**
   * On restart called when the app is being restarted on the screen
   */
  @Override
  public void onRestart() {
      super.onRestart();
  
  
  
      // must prompt with modal dialog for pin
  
      final Dialog dialog = new Dialog(this);
  
      dialog.setCancelable(false);
  
      // check pin
      dialog.setCanceledOnTouchOutside(false);
      // set view to enterpin XML screen
      dialog.setContentView(R.layout.enterpin);
  
  
      // show dialog
      if (!dialog.isShowing()) {
          dialog.show();
      }
      // listen for button being clicked
      Button button = (Button) dialog.findViewById(R.id.pinlogin);
      button.setOnClickListener(new OnClickListener() {// anonymous inner
          // class
          // implementation
          @Override
          public void onClick(View v) {
              EditText editText = (EditText) dialog
                      .findViewById(R.id.enterpintext);
              try {
                  int enteredPin = Integer.parseInt(editText.getText()
                          .toString());
                  SharedPreferences sharedP = Prefs.get(WebViewActivity.this);
                  int temp = sharedP.getInt("pin", 0);
                  if (enteredPin == temp) {
                      pinCheck = true;
  
  
  
                      dialog.dismiss();
                  } else {
                      pinCheck = false;
                      dialog.setTitle("Incorrect Pin");
                  }
              } catch (NumberFormatException e) {
                  Dialog dialog2 = new Dialog(WebViewActivity.this);
                  dialog2.setTitle("Enter Numbers Only");
                  dialog2.setCanceledOnTouchOutside(true);
                  dialog2.show();
              }
          }
      });
  
  }
  </code></pre>
  
  <p>Is there somewhere I could move my Dialog initialization without feeling like it's bad programming practice? I understand why the dialog.isShowing() method that I tried doesn't work because my Dialog instance only exists for the life of the method.</p>
  
  <p>I also notice that should you turn the phone 90 degrees from vertical to horizontal orientation, and the otherway around, my dialogs disappear. Could someone point out the chain of methods called when this forced redraw happens so I can redraw my dialogs?</p>