1

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

private class async extends AsyncTask<String, Void, Boolean> {
    final ProgressDialog progressDialog = new ProgressDialog(getParent());

    @Override
    protected Boolean doInBackground(String... params) {

        GetJson json = new GetJson();
        boolean success = false;

        JSONObject mJsonObject = json
            .readJsonObject("url");
        try {
            success = mJsonObject.getBoolean("success");
        } catch (Exception e) {
        }
        return success;
    }

    @Override
    protected void onPostExecute(Boolean result) {

        if (result) {
            if (progressDialog.isShowing())
                progressDialog.dismiss();
            }
        }
    }

    @SuppressWarnings("static-access")
    @Override
    protected void onPreExecute() {
        progressDialog.show(getParent(), "Working..", "Please wait...");
    }
}
5
  • 1
    Please show us only the relevant part of your code where you try to dismiss the dialog. Commented Jun 13, 2011 at 6:58
  • Have you checked if the boolean result ever becomes true? You don't handle a possible exception at all which is a bad thing. Commented Jun 13, 2011 at 7:09
  • ya i had checked the boolean value is coming true. Commented Jun 13, 2011 at 7:13
  • So in onPostExecute() the parameter result is definitely true? Commented Jun 13, 2011 at 7:17
  • Check what progressDialog.isShowing() is returning ? Commented Jun 13, 2011 at 7:26

5 Answers 5

6
private final class YourTask extends AsyncTask<Void, Void, Object> {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(YourActivity.this, "Title", "Message", true);
    }

    @Override
    protected Object doInBackground(final Void... params) {
        // Doing something
    }

    @Override
    protected void onPostExecute(final Object result) {
        // Check result or something
        dialog.dismiss();
    }

}
3
  • Are Egor's and Alex's different? Commented Jun 13, 2011 at 6:57
  • @PMPareshMayani: Yes, they are lacking code examples. However his answer is missing an explanation. Commented Jun 13, 2011 at 6:59
  • ya its true because i m passing always the correct username and password and i debugged also to check along with sysout. Commented Jun 13, 2011 at 7:23
3

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

3

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

0

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.

final ProgressDialog progressDialog = new ProgressDialog(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=new ProgressDialog();
dialog.setMessage("Loading");
dialog.show(getParent());

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

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

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.