1

I have an AlertDialog.Builder displaying on an application. When I rotate the screen, I get "application has leaked window" error. How do I cancel the AlertDialog in the onPause() event? I don't see any method for Ad.cancel().

Update: code edited for working model.

   private AlertDialog inputDlg; 
   ...
   @Override
   protected void onPause ()
   {
     // close dialog if it's showing
     if (inputDlg != null)
        if (inputDlg.isShowing ())
          inputDlg.dismiss ();

      super.onPause ();
   }

   ...

   private void dlgUserPrompt (Context c)
   {
      // Set an EditText view to get user input 
      final EditText input = new EditText (c);

      AlertDialog.Builder Ab = new AlertDialog.Builder (this);
      Ab.setTitle("title");
      Ab.setMessage("I won't explode if you rotate this");
      Ab.setView(input);
      inputDlg  = Ab.show ();
   }
wufoo
  • 13,571
  • 12
  • 53
  • 78

2 Answers2

1

I think you're looking for the dismiss method.

Chris
  • 22,923
  • 4
  • 56
  • 50
  • Actually, when using AlertDialog.Builder, you don't get a simple dismiss method. After reading Kurtis's reply, it looks like the solution is actually quite complex. – wufoo Nov 08 '11 at 18:01
  • 1
    The show() method on AlertDialog.Builder returns an AlertDialog object that does indeed have a dismiss method. Unless you're doing something very computationally intensive in your view, you're better off just letting Android kill/restart your activity on configuration change rather than overriding this in your manifest as it is a simpler solution. – Chris Nov 08 '11 at 18:25
0

You need to override the onConfigurationChange() hook to do this. This document will help explain to you more about what a configuration change is exactly and how to handle them.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102