即使该活动已经破坏的AsyncTask不会停止 [英] AsyncTask won't stop even when the activity has destroyed

查看:138
本文介绍了即使该活动已经破坏的AsyncTask不会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有开始创建活动时,执行和做的东西在后台(下载100幅图像)的AsyncTask的对象。一切工作正常,但有这我不能够理解这种奇特的行为。

I have an AsyncTask object which starts executing when the activity is created and does stuff in the background (downloads up to 100 images). Everything works fine but there is this peculiar behavior which i'm not able to understand.

有关,例如:当机器人屏幕的方向变化,然后该活动被破坏并重新创建。所以我重写onRetainNonConfigurationInstance()方法,并保存在AsyncTask的执行的所有下载的数据。我这样做的目的是没有的AsyncTask运行过程中的方向变化,每次活动被销毁创建的,但我可以在我的日志中看到previous的AsyncTask仍在执行。 (该数据是正确的,虽然保存)

For eg: when the android screen's orientation changes then the activity is destroyed and created again. So I override the onRetainNonConfigurationInstance() method and save all the downloaded data executed in the AsyncTask. My purpose of doing this is to not have AsyncTask run each time activity is destroyed-created during orientation changes, but as i can see in my logs the previous AsyncTask is still executing. (The data is saved correctly though)

我甚至试图取消的AsyncTask中的onDestroy()活动的方法,但日志仍然显示AsyncTask的作为运行。

I even tried to cancel the AsyncTask in the onDestroy() method of the activity but the logs still show AsyncTask as running.

这是非常奇怪的行为,真的很感激,如果有人能告诉我正确的步骤来停止/取消AsyncTask的。

This is really strange behavior and would really be thankful if someone can tell me the correct procedure to stop/cancel the AsyncTask.

感谢

推荐答案

AFAIK,你不能停止的AsyncTask 。我不相信取消()方法将工作,如果该任务已经执行 - 这将工作,如果该任务是在队列中等待的线程释放

AFAIK, you cannot stop an AsyncTask. I do not believe that the cancel() method will work if the task is already executing -- that will work if the task is waiting in a queue for a thread to free up.

这篇关于即使该活动已经破坏的AsyncTask不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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