2

I would like to know if it is possible to get 'leftover' calls to AsyncTask#onProgressUpdate after AsyncTask#onPostExecute has been called? I am setting text on the same TextView using both of them, and I don't want to set text such as "Done!" and then have it overwritten at a later point by text such as "Almost there - 90%"

Also, I am assuming that the onProgressUpdate method works similar to a SwingWorker method in that multiple calls to publishProgress may stack up before a call to onProgressUpdate occurs. I would really like to know where the "newer" and "older" progress updates are on the parameter - aka are the newest updates at position 0 in the parameter, or at position progress.length?

Hamy
  • 20,662
  • 15
  • 74
  • 102

2 Answers2

2

The code to publishProgress is:

protected final void publishProgress(Progress... values) {
    sHandler.obtainMessage(MESSAGE_POST_PROGRESS,
            new AsyncTaskResult<Progress>(this, values)).sendToTarget();
}

The sendToTarget call uses Handler#sendMessage which the docs say "pushes the message onto the end of the message queue." I understand that to mean that you will not get any out of order updates and that they will stack up. Also, for each publishProgress call there will be a corresponding onProgressUpdate call.

Rich Schuler
  • 41,814
  • 6
  • 72
  • 59
  • Interesting! Thanks for taking the time to look this up! Guess it doesn't work the same way as the SwingWorkers :) – Hamy Jun 29 '10 at 04:24
0

Hum, I'm sorry but I will answer it regardless of the previous answer : YES, you are guaranteed that onPostExecute() will be invoked AFTER all calls to onProgressUpdate().

Why? Because the doInBackground is responsible for invoking onProgressUpdate(), so it can't happen outside of it. onPostExecute() is invoked after the result has been retrieved, since it does transmit it to onPostExecute()

Regards

Nabil Gardon
  • 175
  • 1
  • 1
  • I don't believe it is guarenteed..do you have any documented support for your claim? This post suggests otherwise: http://stackoverflow.com/questions/8219115/asynctask-never-executes-onpostexecute – bhekman Jul 19 '12 at 23:23
  • Of course I didn't mentioned it but here it is: it is guaranteed in all cases BUT the one in wich a cancel was called in between. In that particular case, onPostExecute is simply never invoked. OnCancel is called instead. That's what your link is about. – Nabil Gardon Sep 10 '12 at 00:44
  • Yep. But wouldn't that mean that onPostExecute is not guaranteed to be invoked after all calls to onProgressUpdate? – bhekman Sep 10 '12 at 20:49