Does onPostExecute
execute if the AsyncTask
has been cancelled?
If it does execute, is it safe to say that I should always ask if the task has been cancelled (isCancelled
) at the start of onPostExecute
, before doing anything else?
Does onPostExecute
execute if the AsyncTask
has been cancelled?
If it does execute, is it safe to say that I should always ask if the task has been cancelled (isCancelled
) at the start of onPostExecute
, before doing anything else?
From my experience the onPostExecute() is actually not invoked when the task is cancelled. However, it may be possible to cancel the task after the task's doInBackground() is finished but before the onPostExecute() is invoked - in this case the onPostExecute() is actually invoked. Therefore, to be sure, I call the isCancelled() method in onPostExecute() and just "return" if the task has been cancelled. It works for me.
After checking the AsyncTask source code it seems that onPostExecute is invoked even if the task is cancelled. However, before calling onPostExecute the result is set to null (?) if the task has been cancelled.
@hgpc have you reported this as a bug? onPostExecute() shouldn't be invoked if the task is cancelled, instead onCancelled() should be invoked