ProgressDialog : how to prevent Leaked Window

南笙酒味 提交于 2019-11-28 09:35:06

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)

Use:

progressDialog.dismiss();

in end work

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

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

cygnus has a good idea to use showDialog(MY_INT), where MY_INT is just some constant value you choose just to distinguish it from any other similar dialogs you launch that way. You take it down the same way with dismissDialog(MY_INT). Just don't launch it from your onPause method. You may want to do that instead from the the onResume method of the activity the user is going to. Then you override that activity's onCreateDialog method like this:

@Override
protected Dialog onCreateDialog(int id) {
    if(id == MY_INT) {
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Your message string");
        return progressDialog;
    }
    return super.onCreateDialog(id);
}
cyngus

Instead of using ProgressDialog.show(), try using

Activity.showDialog() which should automatically manage the Dialog for you and prevent leaks.

EDIT: When you call showDialog(int), it will trigger Activity.onCreateDialog(int) where you can create the Dialog you want and return the Dialog you want to display.

Andy Dyer

I ran into a similar problem with a progress dialog and a background task. The AsyncTask (http://android-developers.blogspot.de/2009/05/painless-threading.html) allowed me to do both much more cleanly and without the leaked window.

Its better to use an AsyncTask to get something from internet in the background. And there is no need to pass a static context though. And activity

new YourAsyncTask(context).execute();

Call AsyncTask like above

private class YourAsynTask extends AsyncTask<String,Void,String>
{
 private Context context;
 private ProgressDialog progressDialog;

 //pass context in constructor
  public YourAsynTask(Context context)
  {
     this.context = context;
  }

  //show progress in onPre 
  @Override
  protected void onPreExecute()
  {
    //show Progress code here.
    progressDialog = ProgressDialog.show(context, "", "Loading. Please wait...", true);
  }

  //dismiss Progress dialog in onPost
  @Override 
  protected void OnPostExecute(String response)
  {
    if(progressDialog!=null)
     progressDialog.dismiss();
     progressDialog = null;
  }
}
Mahesh

If you are using any thread or AsyncTask and downloading stuff from internet and showing progress bar , you should use DialogFragment or cancel progress dialog when Activity get stop also if you are showing progress in Asynctask fist cancel Asynctask and override oncancel callback method and dismiss progress dialogue there.

Window leak in Activity or fragment is actually occurs due to you are trying to add a window and while it shows up it is on the foreground,but when you are pressing the home it gets paused and then gets stopped via the onStop(). So your CustomView remains attached to the window that now has disappeared.Hence according to the system your customView occupied the space which it did not release.

Try calling progressDialog.dismiss() before the activity gets killed. I got mine fixed like this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!