I have implemented AsyncTask in my one of activity:
performBackgroundTask asyncTask = new performBackgroundTask();
asyncTask.execute();
Now, i need to implement the "Cancel" button functionality, so i have to stop the execution of the running task. I don't know how do i stop the running task(background task).
So Please suggest me, how do i cancel the AsyncTask forcefully ?
Update:
I found about the Cancel()
method of the same, but i found that calling cancel(boolean mayInterruptIfRunning)
doesn't necessarily stop the execution of the background process. All that seems to happen is that the AsyncTask will execute onCancelled(), and won't run onPostExecute() when it completes.
Our global AsyncTask class variable
And KEYCODE_BACK action which interrupt AsyncTask
It works for me.
Even though an AsyncTask should not be used for long running operations, sometimes it may be caught in a task that does not respond (such as a non-responding HTTP call). In that case, it may be necessary to cancel the AsyncTask.
We have to challenges in doing this. 1. The usual progress dialog displayed with an AsyncTask is the first thing cancelled on an AsyncTask when the back button is pressed by the user. 2. The AsyncTask may be in the doInBackground method
By creating a dismissDialogListerner on the ProgressDialog, a user can press the back button and actually nullify the AsycnTask and close the dialog itself.
Here is an example:
It really depends on what you are doing in your asynctask.
If it's a loop processing a lot of files, you can just check after each files if the isCanceled() flag is raised or not and then break from your loop if it is.
If it's a one line command that performs a very long operation, there's not much you can do.
The best workaround would be to not use the cancel method of the asynctask and use your own cancelFlag boolean. You can then test this cancelFlag in your postExecute to decide what to do with the result.
Call
cancel()
on theAsyncTask
. Whether or not this will actually cancel anything is dependent a bit upon what you are doing. To quote Romain Guy:The mentioned in comments case that
isCancelled() always returns false even i call asynctask.cancel(true);
is especially harmful if I close my app, but the AsyncTask continues working.To solve this I modified the proposed by
Jacob Nordfalk
code in the following way:and added the following to the main activity:
As my AsyncTask was a private class of one of views, so getters or setters of the flag were necessary to inform the AsyncTask about the currently actual flag value.
My multiple tests (AVD Android 4.2.2, Api 17) have shown that if an AsyncTask is already executing its
doInBackground
, thenisCancelled()
reacts in no way (i.e. continues to be false) to any attempts to cancel it, e.g. duringmViewGroup.removeAllViews();
or during anOnDestroy
of theMainActivity
, each of which leads to detaching of viewsIf I manage to force stopping the
doInBackground()
thanks to the introducedFlagCancelled
, thenonPostExecute()
is called, but neitheronCancelled()
noronCancelled(Void result)
(since API level 11) are not invoked. (I have no idea why, cause they should be invoked andonPostExecute()
should not, "Android API doc says:Calling the cancel() method guarantees that onPostExecute(Object) is never invoked." -IdleSun
, answering a similar question).On the other hand, if the same AsyncTask hadn't started its
doInBackground()
before cancelling, then everything is ok,isCancelled()
changes to true and I may check that inJust check
isCancelled()
once in a while: