Skip to content Skip to sidebar Skip to footer

How Can I Dismiss Progressdialog In Asynctask

I am using a ProgressDialog to be shown while my background process goes on, but after background process is completed the ProgressDialog is not dismissed still. Here is my code pr

Solution 1:

private final classYourTaskextendsAsyncTask<Void, Void, Object> {
    privateProgressDialog dialog;

    @OverrideprotectedvoidonPreExecute() {
        dialog = ProgressDialog.show(YourActivity.this, "Title", "Message", true);
    }

    @OverrideprotectedObjectdoInBackground(final Void... params) {
        // Doing something
    }

    @OverrideprotectedvoidonPostExecute(final Object result) {
        // Check result or something
        dialog.dismiss();
    }

}

Solution 2:

You can call progressDialog.dismiss() in your AsyncTask's onPostExecute() method.

Solution 3:

In onPostExecute() method call dismiss() on your dialog.

Solution 4:

I dont know, if you solved this problem, probably yes.

But for next users what will have the same problem (i had it right now too)..

The problem is in your declaration.

finalProgressDialogprogressDialog=newProgressDialog(getParent());

or in your "second" declaration

progressDialog.show(getParent(), "Working..", "Please wait...");

Because in the first one you put in there the getParent parameter (probably MainActivity.this) and the same you do in calling show method. Therefore is there 2 parents.. and if you call dismiss() in post execute..it dismiss the first one..but not the another one what have then dialog.isShowing() equal to false.

So important is have there just 1!!!!! parent content..so you can assign the content in declaration with:

new ProgressDialog(content);

or you can do

ProgressDialog dialog=newProgressDialog();
dialog.setMessage("Loading");
dialog.show(getParent());

and then dismiss it and everything is allright. or you can do:

ProgressDialog dialog=newProgressDialog(getParent());
dialog.setMessage("Loading");
dialog.show();

but never give in there twice parents, contents, whatever..

Solution 5:

AsyncTasks should not handle at ALL a dialog. Dismissing the dialog in the PostExecute phase can easily lead to an IllegalStateException as the underlying activity can already be destroyed when the dialog gets dismissed. Before destroying the activity the state of the activity will be saved. Dismissing the dialog afterwards will lead to an inconsistent state.

Post a Comment for "How Can I Dismiss Progressdialog In Asynctask"