Skip to content Skip to sidebar Skip to footer

Canceling Asynctask That Is Not Running Calls Oncancelled?

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. What i did is put

Solution 1:

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:

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

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().

Post a Comment for "Canceling Asynctask That Is Not Running Calls Oncancelled?"