0

I am getting WindowLeakedError while trying to show ProgressDialog in onPreExecute in AsyncTask. Some Stackoverflow threads have mentioned there could be the case that Activity is finishing and I try to show ProgressDialog in same Activity this might cause this exception but I have put a check that if activity is infinishing state`, dont show it.

WindowLeaked Exception

05-23 10:43:27.528: E/WindowManager(21108): Activity com.example.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40dd0968 that was originally added here
05-23 10:43:27.528: E/WindowManager(21108): android.view.WindowLeaked: Activity com.example.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40dd0968 that was originally added here
05-23 10:43:27.528: E/WindowManager(21108):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:351)
05-23 10:43:27.528: E/WindowManager(21108):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:373)
05-23 10:43:27.528: E/WindowManager(21108):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:321)
05-23 10:43:27.528: E/WindowManager(21108):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:152)
05-23 10:43:27.528: E/WindowManager(21108):     at android.view.Window$LocalWindowManager.addView(Window.java:541)
05-23 10:43:27.528: E/WindowManager(21108):     at android.app.Dialog.show(Dialog.java:301)
05-23 10:43:27.528: E/WindowManager(21108):     at com.triplebottomline.ezmsds.web.FetchDataTask.onPreExecute(FetchDataTask.java:47)
05-23 10:43:27.528: E/WindowManager(21108):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
05-23 10:43:27.528: E/WindowManager(21108):     at android.os.AsyncTask.execute(AsyncTask.java:511)
05-23 10:43:27.528: E/WindowManager(21108):     at com.example.MyActivity$4.onClick(MyActivity.java:467)

Please see the following code:

AsyncTask Class

public class FetchDataTask extends AsyncTask
{
    private Activity callerActivity;
    private ProgressDialog dialog;

    public FetchDataTask (Activity activity)
    {
        callerActivity = activity;

    }

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(callerActivity);
        dialog.setCancelable(false);
        if(callerActivity != null && !callerActivity.isFinishing())
        {
            dialog.show();
        }
    }

@Override
    protected void onPostExecute(Object result) {
        dialog.dismiss();
        dialog = null;
          }

Whole Activity code is too large. But I am posting the code which is triggering the AsyncTask on Button Click

AsyncTask Caller Code

FetchDataTask task = new FetchDataTask(MSDSCategories.this);                        
task.execute(TaskType.DOWNLOAD_PDF);

Please help and point me out what can be the issue.

Thanks in advance.

Regards,

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46

1 Answers1

2

You're holding a strong reference to your Activity, which keeps it from getting garbage collected. Use a weak reference instead:

public class FetchDataTask extends AsyncTask
{
    private WeakReference<Activity> callerActivity;
    private WeakReference<ProgressDialog> dialog;

    public FetchDataTask (Activity activity) {
        callerActivity = new WeakReference<Activity>( activity );
    }

    @Override
    protected void onPreExecute() {
        if( callerActivity.get() != null ) {
            dialog = new WeakReference<ProgressDialog>( new ProgressDialog( callerActivity.get() ) );
            dialog.get().setCancelable(false);
            if( callerActivity.get() != null && !callerActivity.get().isFinishing()) {
                dialog.get().show();
            }
        }
    }

    @Override
    protected void onPostExecute(Object result) {
        if( dialog.get() != null )
            dialog.get().dismiss();
        dialog = null;
    }
323go
  • 14,143
  • 6
  • 33
  • 41