Wednesday, March 7, 2012

Leaked window Exception When using Progress Dialog in Android

Views have a reference to their parent Context (taken from constructor argument). If you leave an Activity without destroying Dialogs and other dynamically created Views, they still hold this reference to your Activity (if you created with this as Context: like new ProgressDialog(this)), so it cannot be collected by the GC, causing a memory leak. To solve this issue , dismiss method of the dialog should be called in on destroy method of that activity.
@Override
protected void onDestroy() {
    runOnUiThread(new Runnable() {
       @Override
       public void run() {
        if (mDialog.isShowing()) {
          mDialog.dismiss();
        }
       }
    });
    super.onDestroy();
 }

No comments:

Post a Comment