问题描述
如果 AsyncTask
被取消了,onPostExecute
会执行吗?
Does onPostExecute
execute if the AsyncTask
has been cancelled?
如果它确实执行了,我是否应该总是在 onPostExecute
开始时询问任务是否已被取消 (isCancelled
),然后再做任何事情还有吗?
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?
推荐答案
在 Android 2 和 Android 4 之间更改了 onPostExecute on cancel() 的记录行为.
The documented behaviour of onPostExecute on cancel() was changed between Android 2 and Android 4.
在 doInBackground 之后在 UI 线程上运行.指定的结果是doInBackground 返回的值,如果任务被取消则返回 null或发生异常.
在 doInBackground 之后在 UI 线程上运行.指定的结果是doInBackground 返回的值.如果出现以下情况,则不会调用此方法任务被取消了.
因此,如果您仍然针对 Android 2 设备,您应该假设将调用 onPostExecute 并在 onPostExecute 检查空结果.
So if you are still targeting Android 2 devices you should assume that onPostExecute will be called and in onPostExecute check for null result.
这篇关于在取消的 AsyncTask 上执行 onPostExecute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!