4

I have a problem showing a progress dialog with an AsyncTask:

Activity has leaked window com.android.internal.policy.PhoneWindow$DecorView{8cee959 V.E...... R......D 0,0-1026,252} that was originally added here

This is my Async Task:

public class MyClientTask extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progressDialog = null;

    private Context mContext;

    MyClientTask(Context mContext) {
        this.mContext = mContext;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(mContext);
        progressDialog.setMessage("Working ...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        try {
            wait(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }

    }
}

And this is my activity:

public class ConnectionActivity extends Activity {

    final private Context mContext = this;
    private Button buttonConnect;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R .layout.activity_socketconnection);

        buttonConnect = (Button) findViewById(R.id.buttonConnect);

        buttonConnect.setOnClickListener(getConnectOncOnClickListener());
    }

    private OnClickListener getConnectOncOnClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                MyClientTask myClientTask = new MyClientTask(mContext);
                try {

                        myClientTask.execute();
                        myClientTask.get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }

            }
        };
    }

}

I searched the solution, but it doesn't work anyway

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
Droide
  • 1,807
  • 2
  • 17
  • 30
  • 1
    Possible duplicate of [android activity has leaked window com.android.internal.policy.impl.phonewindow$decorview Issue](http://stackoverflow.com/questions/12000940/android-activity-has-leaked-window-com-android-internal-policy-impl-phonewindow) – Tigger Jul 14 '16 at 08:47
  • @Tigger I read it, but it's not my problem.. that solution doesn't work – Droide Jul 14 '16 at 08:48

4 Answers4

2

A couple of hints:

  1. Keep the reference to the ProgressDialog in your activity and add public methods to show/hide it;

  2. Use a WeakReference<Context> in your AsyncTask to let the garbage collector do it's job;

  3. Get rid of the myClientTask.get() that blocks the UI thread.

Droide
  • 1,807
  • 2
  • 17
  • 30
dev.bmax
  • 8,998
  • 3
  • 30
  • 41
1

This usually happens when you are finishing the activity without calling dismiss() or hiding the dialog first. I suggest you to rework your code a bit, creating the actual ProgressDialog in your Activity then storing a reference to it. In your onDestroy(), as a precaution check if it is not null and showing, then dismiss() it.

Reaper
  • 21
  • 1
0

The problem is calling wait() in background. Please have a look here.

Sample:

 private Object lock = new Object();

    @Override
    protected Void doInBackground(Void... arg0) {

        synchronized(lock) {
            try {
                lock.wait(5000);
                //  Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return null;
    }
Community
  • 1
  • 1
kalpana c
  • 2,739
  • 3
  • 27
  • 47
0

use progressDialog.cancel(); instead of progressDialog.dismiss();

you can find difference b/w them here:

What is the difference between a dialog being dismissed or canceled in Android?

Community
  • 1
  • 1
Jaydeep Devda
  • 727
  • 4
  • 18