문제

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();  
}
도움이 되었습니까?

해결책

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.

다른 팁

use asyncTask.

add your code in doinbackground();

show dialog in preexecute();

dismiss progress dialog after complete your doinbackground();

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top