Question

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();  
}
Was it helpful?

Solution

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.

OTHER TIPS

use asyncTask.

add your code in doinbackground();

show dialog in preexecute();

dismiss progress dialog after complete your doinbackground();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top