android.view.WindowLeaked exception

12,845

Solution 1

When a dialog on an activity is set to visible but on orientation changes the activity itself is destroyed, then it causes leaked window error.

There are two methods to handle this situation:-

Method 1
Therefore,you need to dismiss dialog in activity's onStop or onDestroy method. For example:

@Override
protected void onStop() {
    super.onStop();

    if(pd!= null)
        pd.dismiss();
}

and define dialog in activity class

ProgressDialog pd;

This link will help you Handling progress dialogs and orientation changes

Method 2
You have to add this to the activity declaration in the manifest:

android:configChanges="orientation"

so it looks like

<activity android:label="@string/app_name" 
        android:configChanges="orientation|keyboardHidden" 
        android:name="com.eisuru.abc.MainActivity">

The matter is that the system destroys the activity when a change in the configuration occurs. See ConfigurationChanges.

So putting that in the configuration file avoids the system to destroy your activity. Instead it invokes the onConfigurationChanged(Configuration) method.

Solution 2

You're trying to show a Dialog after you've exited an Activity.

The solution is to call dismiss() on the Dialog you created in Example.java:183 before exiting the Activity, e.g. in onPause(). All windows&dialogs should be closed before leaving an Activity.

Or

Add this to your manifest:

android:configChanges="orientation|keyboardHidden

Then in your activity add this somewhere:

 @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
  //ur Code
}
 if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    //ur Code
} 
}

Solution 3

dialog is a child that are belong to main thread and if you want to show or kill them you must do this on OnUiThread like this. I am using fragment and when I show dialog, get this exception. But this method save me.

 getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            pDialog.show();//dismiss any dialog like this
        }
    });
Share:
12,845
Isuru
Author by

Isuru

Updated on July 28, 2022

Comments

  • Isuru
    Isuru almost 2 years

    I'm reading xml data from a url. It worked well when it was it portrait mode. But I wanted to change it to landscape mode. But it gets android.view.WindowLeaked exception.

    Please help me with this. Thanks in advance. This is my code.

    package com.eisuru.abc;
    
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.pm.ActivityInfo;
    import android.view.Menu;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        TextView tvResponse;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    
            tvResponse = (TextView) findViewById(R.id.tvResponse); 
            new PostAsync().execute(); 
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    
        class PostAsync extends AsyncTask<Void, Void, Void> { 
            ProgressDialog pd; XMLHelper helper;     
            @Override 
            protected void onPreExecute() { 
                pd = ProgressDialog.show(MainActivity.this, "Exchange Rates", "Loading Exchange rates values ...", true, false);
                } 
    
            @Override 
            protected Void doInBackground(Void... arg0) { 
                helper = new XMLHelper(); helper.get(); 
                return null; 
                }   
    
            @Override 
            protected void onPostExecute(Void result) 
            { 
                StringBuilder builder = new StringBuilder(); 
                for(Exrate_values post : helper.exrates) {
    
                    builder.append("\n\t " + post.getDate()); 
                    builder.append("\t \t\t " + post.getFrom_currency()); 
                    builder.append("\t \t\t " + post.getTo_Currency()); 
                    builder.append("\t \t\t " + post.getExrt_buy()); 
                    builder.append("\t \t\t\t " + post.getExrt_sell()); 
    
    
                    builder.append("\n"); 
    
            } 
                    tvResponse.setText(builder.toString()); 
                    pd.dismiss(); 
                    }   
            } 
    
    }
    
  • Isuru
    Isuru almost 10 years
    Thank you very much for your useful comment. I did the second method. It worked very well. android:screenOrientation="landscape" android:configChanges="keyboard|keyboardHidden|orientation|s‌​creenLayout|uiMode|s‌​creenSize|smallestSc‌​reenSize">
  • Giru Bhai
    Giru Bhai almost 10 years
    @Isuru Happy to help,enjoy coding.
  • LargeGlasses
    LargeGlasses over 9 years
    Method 1 worked perfectly for me. Interesting side note: I already had the orientation declared in the configChanges in my manifest when I was getting the window leak originally.
  • Nico Dumdum
    Nico Dumdum over 9 years
    Method 1 works perfectly and I would definitely prefer it instead of messing with the manifest file.
  • beresfordt
    beresfordt almost 9 years
    could you edit your answer and explain how this resolves the problem?
  • MaxGyver
    MaxGyver over 7 years
    Please check your link "Handling progress dialogs and orientation changes". The linked page shows only advertisement.