在取消的 AsyncTask 上执行 onPostExecute [英] onPostExecute on cancelled AsyncTask

查看:20
本文介绍了在取消的 AsyncTask 上执行 onPostExecute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 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.

Android 2.3.7 onPostExecute :

在 doInBackground 之后在 UI 线程上运行.指定的结果是doInBackground 返回的值,如果任务被取消则返回 null或发生异常.

Runs on the UI thread after doInBackground. The specified result is the value returned by doInBackground or null if the task was cancelled or an exception occured.

Android 4.0.1 onPostExecute :

在 doInBackground 之后在 UI 线程上运行.指定的结果是doInBackground 返回的值.如果出现以下情况,则不会调用此方法任务被取消了.

Runs on the UI thread after doInBackground. The specified result is the value returned by doInBackground. This method won't be invoked if the task was cancelled.

因此,如果您仍然针对 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆