mcfly soft
mcfly soft

Reputation: 11645

ProgressDialog is not closed

I managed to show a ProgressDialog, but I can't close it after my long running process. Does anyone know what is wrong here.

All methods are directly under my onCreate() of my Activity.

This is my snippet of the code.

private final Handler updateHandler = new Handler() {
        public void handleMessage(final Message msg) {
        dismissWaitDialog();

    };


    public void showWaitDialog(String sTitle, String sMessage){
        waitDialog=new ProgressDialog(this); 
        waitDialog.show(this, sTitle, sMessage);
    }

    public void dismissWaitDialog(){
        waitDialog.dismiss();
    }


public void longrunner(){
    showWaitDialog("Please wait","Please wait");

    new Thread() {
        public void run() {
            do something long
            updateHandler .sendMessage(mymessageinit);
        }
    }.start();  
}

Upvotes: 1

Views: 183

Answers (2)

Anil Kanani
Anil Kanani

Reputation: 270

use asyncTask.

add your code in doinbackground();

show dialog in preexecute();

dismiss progress dialog after complete your doinbackground();

Upvotes: 2

laalto
laalto

Reputation: 152817

You're using a static show() method that returns an instance of ProgressDialog that you throw away. The dialog you dismiss is not the same that is showing. To fix, change:

waitDialog=new ProgressDialog(this); 
waitDialog.show(this, sTitle, sMessage);

to

waitDialog = ProgressDialog.show(this, sTitle, sMessage);

As mentioned by others, an AsyncTask is really the canonical way to perform operations like this instead of working with Threads directly.

Upvotes: 2

Related Questions