取消未运行的呼叫onCancelled AsyncTask的? [英] Canceling AsyncTask that is not running Calls onCancelled?

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

问题描述

我有一个从活动A.执行的AsyncTask的乙
在执行后的B时,在一定条件下B可能创造自己的新实例并执行它。

I have an Asynctask B that is executed from activity A. On post execute of that B, on a certain condition B might create a new instance of itself and execute it.

我所做的就是把AsyncTask的声明作为B内部全局和重写onCancelled的B取消一个onpostexecute创建新实例。

What i did is put the AsyncTask declaration as global inside the B and overridden the onCancelled of B to cancel the new instance that is created onpostexecute.

左右)活动的onDestroy一个b.cancel(也将取消任何AsyncTask的执行onPostExecute。

so onDestroy of Activity A b.cancel() will also cancel the any AsyncTask executed onPostExecute.

  @Override
   protected void onCancelled()
   {
      if(currentPlansAsync != null)
      {
         currentPlansAsync.cancel(true);
      }
      super.onCancelled();
   }

和currentPlansAsync的新实例称为onPostExecute如下:

And new instance of currentPlansAsync is called onPostExecute as follows:

 private void processAfterFailure()
   {

      if(this.counter < 3)
      {
         ProgressDialog progressDialog =
            GeneralUtils.showProgressDialog(parentActivity, parentActivity.getResources().getString(string.WaitDialogTitle),
               parentActivity.getResources().getString(string.WaitDialogMessage));
         this.counter++;
         currentPlansAsync = new CurrentPlansAsync(parentActivity, application, progressDialog, application.planBL, this.counter);
         currentPlansAsync.execute();

      }
      else
      {
         Toast.makeText(parentActivity, parentActivity.getResources().getString(string.SERVICE_ERROR), Toast.LENGTH_LONG).show();
      }

   }

我的问题是,如果AsyncTask的B已经停止执行,并已执行另一个AsyncTask的C和活性的去的onDestroy和B被取消( b.cancel(真);
将onCancelled是在B类中重写被称为??

这是所以我保证我取消从活动执行的AsyncTask的同时又有AsyncTask的

This is so i ensure that i cancel the AsyncTask that was executed from the activity and also the AsyncTask executed from within that AsyncTask

推荐答案

onCancelled()终止后立即叫 doInBackground()的回报,如果一个取消()已经调用后台作业完成之前。
如果您取消它不会被称为()的AsyncTask 之后。

onCancelled() is called immediately after the doInBackground() returns, if a cancel() call has been made before the background job has finished. It will not be called if you cancel() the AsyncTask after that.

我建议创建一个公共的方法,会做什么,将取消所有的子任务,并调用这个公共的方法:

I would recommend to create a public method that will do what will cancel all child tasks, and call this public method:

public void cancelChildTasks()
{
    if(currentPlansAsync != null)
    {
        currentPlansAsync.cancel(true);
        currentPlansAsync.cancelChildTasks();
    }
}

您应该仍然可以调用和处理取消()的AsyncTask S的责令停止<方法code> doInBackground如果是在执行过程中()执行。只是应对公共方法的子任务,而不是在 onCancelled()

You should still call and handle the cancel() method of the AsyncTasks in order to stop the doInBackground() execution if it is in the middle of execution. Just deal with the child tasks in the public methods instead of in the onCancelled().

这篇关于取消未运行的呼叫onCancelled AsyncTask的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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