简体   繁体   中英

ProgressDialog in AsyncTask dismiss not working

I have an android-application with Service-classes wo communicate with a RestWebService. The Methods of the Service-classes use AsyncTasks. I want to show a ProgressDialog during the execution of the AsyncTask. Therefore I have a private field

public final class UserService extends Service {
    private static final String LOG_TAG = UserService.class.getSimpleName();
    private static UserService instance = null;
    private ProgressDialog progressDialog;

    public static UserService getInstance() {
        if (instance == null) {
            instance = new UserService();
        }

        return instance;
    }
...

}

It is instanced in the method showProgressDialog(Context ctx):

private ProgressDialog showProgressDialog(Context ctx) {
    progressDialog = new ProgressDialog(ctx);
    progressDialog.setProgressStyle(STYLE_SPINNER);
    progressDialog.setCancelable(true);
    progressDialog.show();
    return progressDialog;
}

I call this method on onPreExecute of my AsyncTask:

@Override
protected void onPreExecute() {
    progressDialog = showProgressDialog(ctx);
}

And try to close it in onPostExecute:

@Override
protected void onPostExecute(HttpResponse<User> unused) {
        Log.v(LOG_TAG, "onPostExecute");
        progressDialog.dismiss();
}

I see, that the onPostExecute method is executed, because the of the Log command, but my ProgressDialog doesn't close.

What am I doing wrong?

Thank you very much in advance for your answer!

如果asynctask是外部类,请确保在preExecute中执行progressdialog.show()之前,已在构造函数中初始化了progressdialog对象。

You are not showing your ProgressDialog correctly .

Upon showing it, call:

private ProgressDialog dialog;

// store the reference, "this" is the Context
dialog = ProgressDialog.show(this, "title...", "message..."); 

upon hiding, use the stored reference :

dialog.dismiss();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM