开发者

AsyncTask onPostExecute not running on main thread?

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, o开发者_运维技巧nPostExcute 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;
        }
    }


Check this:

  1. Make sure you called asynctack.execute() on UI thread.
  2. Use @Override on onPostExcute() to make sure it's properly defined.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜