0

Many of us have come across the dreaded

E/WindowManager﹕ android.view.WindowLeaked

This can happen for several reasons as is mentioned here and in many other SO questions.

I have tried making sure my dialogs are in a "good place" before showing them, often wrapping them around certain conditions like:

if (!isFinishing() && !isDestroyed()) {
    mDialog.show();
}

I'm curious if anyone knows of a "perfect environment" for which an AlertDialog is guaranteed to always display without error. From personal experience, even with the above conditions, the WindowLeaked error still always comes up.

Community
  • 1
  • 1
The Hungry Androider
  • 2,274
  • 5
  • 27
  • 52

1 Answers1

0

This really is a problem I faced when changing the orientation of the Activity, whenever we try to display a dialog with a closed Activity this error will skyrocket. In a less important case, as in a Activity data recording information, I simply ignore the Dialog and closing with the following code:

@Override
public void onPause() {
    super.onPause();  
    try  {  
            if  (myAlertDialog != null && myAlertDialog.isShowing())  {  
                myAlertDialog.dismiss();  
                myAlertDialog = null ;  
            }  
        }  catch  (Exception  e)  { 
            e.printStackTrace(); 
        }
}

But if you need to keep it on the screen, the only way I found was locking the orientation of Activity.

Junior Maia
  • 17
  • 1
  • 12