4

I have a few Activities on my app that hit a web service. Since I don't want to hold up the main thread, this code is in an AsyncTask. However, I do not want the user to be manipulating the Activity while the web service is being called, so before executing the AsyncTask I show a ProgressDialog which spins and blocks the screen. In the onPostExecute method of the AsyncTask, the first thing I do is dismiss the ProgressDialog.

This should prevent the user from manipulating the Activity without actually blocking the main thread.

However, I've noticed a few times where the ProgressDialog is never dismissed and the user becomes stuck. The AsyncTask has finished, onPostExcute has executed, but the ProgressDialog is still shown. The ProgressDialog has no chance of ever being dismissed and the user is stuck on the screen. Their only option is to visit the Android Settings app and force stop my application.

Does anyone have any idea why this happens? What can I do to fix it?

Relevant code:

This is how I show the ProgressDialog and launch the task:

mProgress = ProgressDialog.show(this, "", "Syncing...", true);
(new MyAsyncTask()).execute(intUserId);

This is the onPostExcute for the task. There is no "@Override"

    protected void onPostExecute(Boolean result) {
        if (mProgress != null) {
            mProgress.dismiss();
            mProgress = null;
        }
    }
Andrew
  • 20,756
  • 32
  • 99
  • 177
  • Sounds like a job for debugger – Peter Knego Sep 15 '11 at 16:30
  • 2
    Can you paste a relevant part of the code you are using? It sometimes has to do with the instance of the dialog that you are dismissing. I mean, something like rotating the phone could lead to recreate a new dialog, and sometimes you will be dismissing another reference of the dialog. – Cristian Sep 15 '11 at 16:31
  • 1
    Also see this post: http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working – Peter Knego Sep 15 '11 at 16:41
  • I've updated the main post with relevant code – Andrew Sep 15 '11 at 16:43
  • 1
    Much cleaner to keep your progress dialog within the `AsyncTask` using `onPreExecute` to show it. – Che Jami Sep 15 '11 at 16:53
  • Do you see any errors in your logcat that would indicate that your ProgressDialog trying to be accessed from a different thread? – hooked82 Sep 15 '11 at 20:53

3 Answers3

1

Check this:

  1. Make sure you called asynctack.execute() on UI thread.
  2. Use @Override on onPostExcute() to make sure it's properly defined.
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
0

Maybe try showing your ProgressDialog in the onPreExecute() Method?

protected void onPreExecute() {
    mProgress = ProgressDialog.show(this, "", "Syncing...", true);
}

Give that a shot and see if it works.

hooked82
  • 6,336
  • 4
  • 41
  • 46
0

Peter Knego posted the following link in a comment under the OP. CommonsWare's solution seems to work well.

Backround task, progress dialog, orientation change - is there any 100% working solution?

Community
  • 1
  • 1
Andrew
  • 20,756
  • 32
  • 99
  • 177