grgvn
grgvn

Reputation: 1329

Problem with a ProgressDialog

I don't understand the following error:

Activity [myActivity] has leaked window
com.android.internal.policy.impl.PhoneWindow$DecorView@4050c3f8 that was
originally added here

Here is my code:

private ProgressDialog progression;
private Handler handler;
private Thread thread;

@Override
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);

    //Conteneur général
    ui = new RelativeLayout(this);

    progression = ProgressDialog.show(this, "SwissParl", "Init", true);
    handler = new Handler() {
        public void handleMessage(Message msg) {
            progression.dismiss();
        }
    };

    thread = new Thread() {

        public void run() {

            firstMethod();

            SecondOne();

            andTheLast();

            handler.sendEmptyMessage(0);
        }
    };

    thread.start();
    setContentView(ui);
}

Apparently, the problem is on the line where I instanciate my ProgressDialog...

Any idea?

Upvotes: 0

Views: 1297

Answers (3)

st0le
st0le

Reputation: 33545

I'm not sure what the problem is, but i suggest you use an AsyncTask

Something like this

private class MyBackgroundTask extends AsyncTask<Void, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(ParentActivity.this);


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

        firstMethod();
        SecondOne();
        andTheLast();

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if(dialog.isShowing())
            dialog.dismiss();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //dialog.setCancelable(false); //depending...
        dialog.setMessage("Please Wait...");
        dialog.show();
    }
}

To execute it, call new MyBackgroundTask().execute() inside onCreate()

It's far cleaner than using a handler,thread combination...

Upvotes: 1

Tanmay Mandal
Tanmay Mandal

Reputation: 40168

private ProgressDialog progression;
private Handler handler;
private Thread thread;

@Override
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);

    //Conteneur général
    ui = new RelativeLayout(this);
    setContentView(ui);


    progression = ProgressDialog.show(this, "SwissParl", "Init", true);
    handler = new Handler() {
        public void handleMessage(Message msg) {
            progression.dismiss();
        }
    };

    thread = new Thread() {

        public void run() {

            firstMethod();

            SecondOne();

            andTheLast();

            handler.sendEmptyMessage(0);
        }
    };

    thread.start();
}

You are trying to show a ProgressDialog before adding the main view to the activity.Use the setContentView before you start the ProgressDialog

Upvotes: 0

neteinstein
neteinstein

Reputation: 17613

That happens normally when the activity is destroyed when showing a dialog, for example rotation or finishing activities while showing dialogs.

Try adding a dismiss dialog onPause of activity.

Upvotes: 1

Related Questions