AsyncTask: Is onPostExecute guaranteed to run after all calls to onProgressUpdate?
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 ha开发者_C百科ve 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
?
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.
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
精彩评论