ProgressDialog : how to prevent Leaked Window ProgressDialog : how to prevent Leaked Window android android

ProgressDialog : how to prevent Leaked Window


Use:

progressDialog.dismiss();

in end work


The leak comes probably from your PixelRainActivity.staticThis attribute. If you’re keeping a reference to an activity, even after that this activity has been destroyed, you have a memory leak.

The easiest way to fix is to use the application’s Context instead. Change your staticThis = this line in the method onCreate() to staticThis = this.getApplicationContext() and it should work (and change the type of staticThis to Context if this is not already the case)


There are situations when you have to verify in onDetach or in onDestroy if the progress dialog is still visible.Like so:

@Overridepublic void onDetach() {    if (mProgressDialog != null && mProgressDialog.isShowing())        mProgressDialog.dismiss();    super.onDetach();}