Monday, April 25, 2011

Android Window Leak

The WindowLeaked exception in the log usually happens when you have some sort of async task that is finishing after the activity that began it is destroyed.
Solution for the above issue, we can dismiss the progress dialog before finish the activity.


Issue :

Activity com.sample.ViewLeakIssueActivity has leaked window com.android.internal.policy.impl.TvWindow$DecorView@6bc30788 that was originally added here
E/WindowManager( 1687): at android.view.ViewRoot.(ViewRoot.java:230)
E/WindowManager( 1687): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
-----

Sample Leak Issue
public class ViewLeakIssueActivity extends Activity {
volatile ProgressDialog pd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pd = ProgressDialog.show(ViewLeakIssueActivity.this, "", "test...", true);

new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("@@@@@@@@@@@"+ViewLeakIssueActivity.this+pd.isShowing());

pd.dismiss();
}
}.start();

finish();

}

}

Solution for the above issue, we can dismiss the progress dialog before finish the activity.

@Override
public void finish() {
if(pd.isShowing()){
pd.dismiss();
}
super.finish();
}


5 comments:

  1. When I google for WindowLeaked exception, I always read about creating dialogs in async tasks, but there's way more simple scenario that causes the issue - opening the dialog in the main UI thread and rotating the device while it's still open. Do I have to keep track of all the open dialogs to call dismiss() in Activity.onStop()?

    ReplyDelete
  2. You are explanation is excellent and nice posting thanks for sharing this in formation can you give me the over view on Android Development.

    ReplyDelete
  3. Great Explainationation, I like this blog.

    ReplyDelete
  4. Your application should get an ANR error

    ReplyDelete