Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: cancel(true) does not kill the AsyncTask

I am using an Android AsyncTask in order to download files from a server. when files are downloaded, I am trying to kill the AsyncTask.

protected void onPostExecute(Void result) {
    MyTask.cancel(true);
}

But it stills running (I can see it from the Debug window in eclipse). How to kill the AsyncTask?

like image 229
user1471575 Avatar asked Dec 13 '25 05:12

user1471575


1 Answers

When the AsyncTask is finished running, onPostExecute() will be called. The thread will die. The garbage collector will clean it up. You don't have to do anything. What you're doing is redundant.

Other than that, calling "Cancel" sends and interrupt signal to the thread. If your process can be interrupted, it will stop blocking and continue to execute. You then have to call isCancelled() in doInBackground() and return from the method if isCancelled() is true. After which, onCanceled() and onPostExecute() will be called and the thread will die on it's own like normal.

like image 103
DeeV Avatar answered Dec 16 '25 07:12

DeeV