views:

163

answers:

3

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?

+1  A: 

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.

Martin Vysny
+1  A: 

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
A: 

@hgpc have you reported this as a bug? onPostExecute() shouldn't be invoked if the task is cancelled, instead onCancelled() should be invoked

Andreas Schildbach
Not sure we should assume this is a bug. It's just not very well documented. :P
hgpc