Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to completely kill/remove/delete/stop an AsyncTask

I made an app that downloads videos from our server. The issue is:

When i cancel the downloading i call:

myAsyncTask.cancel(true)

I noticed, that myAsyncTask doesn't stops on calling cancel... my ProgressDialog still goes up and its like jumping from status to status showing me that each time I cancel and start again an AsyncTask by clicking the download button, a new AsyncTask starts... Each time I click download.. then cancel, then again download a separate AsyncTask starts.

Why is myAsynTask.cancle(true) not cancelling my task ? I don't want it anymore on the background. I just want to completely shut it down if I click cancel.

How to do it ?

E D I T:

Thanks to gtumca-MAC, and the others who helped me did it by:

while (((count = input.read(data)) != -1) && (this.isCancelled()==false)) 
{
    total += count;
    publishProgress((int) (total * 100 / lenghtOfFile));
    output.write(data, 0, count);
}

Thanks!!!

like image 963
Adam Varhegyi Avatar asked Jun 04 '12 13:06

Adam Varhegyi


People also ask

How to terminate AsyncTask?

cancel(true); This example demonstrate about how to cancel an executing AsyncTask in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

How do I stop AsyncTask when activity is destroyed?

You can either cancel the AsyncTask in the onStop method of your activity or you can let your async task finish, and not loose its progress and relink it to the next instance of your activity.

How to kill Async task in android?

After researching the problem on Stack Overflow, I adopted the following solution: volatile boolean running; public void onActivityCreated (Bundle savedInstanceState) { super. onActivityCreated(savedInstanceState); running=true; ... } public void onDestroy() { super. onDestroy(); running=false; ... }

Can we cancel async task?

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns.


3 Answers

AsyncTask does not cancel process on

myAsynTask.cancel(true)  

For that you have to stop it manually.

for example you are downloading video in doInBackground(..) in while/for loop.

protected Long doInBackground(URL... urls) {           for (int i = 0; i < count; i++) {           // you need to break your loop on particular condition here               if(isCancelled())                   break;                       }          return totalSize;      } 
like image 118
MAC Avatar answered Oct 10 '22 04:10

MAC


Declare in your class

DownloadFileAsync downloadFile = new DownloadFileAsync(); 

then On Create

DownloadFileAsync downloadFile = new DownloadFileAsync(); downloadFile.execute(url); 

in Your Background ()

if (isCancelled())     break;  @Override protected void onCancelled(){  } 

and you can kill your AsyncTask by

downloadFile.cancel(true); 
like image 21
Mohamed Hussien Avatar answered Oct 10 '22 05:10

Mohamed Hussien


When you start a separate thread(AyncTask) it has to finish. You have to manually add a cancel statement to your code inside the AsyncTask.

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

Checkout more in the documentation: http://developer.android.com/reference/android/os/AsyncTask.html

like image 31
Zelleriation Avatar answered Oct 10 '22 05:10

Zelleriation