2

Is there anyway to cancel all the running instances of an Asynctask ? Thanks in advance.

I want to cancel the progress dialog on pressing back button meanwhile all the instances of an asynctask from the same piece of code.

Awadesh
  • 3,530
  • 2
  • 20
  • 32
  • 1
    Call the `cancel` method. Refer to this: http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean) – Thomas R. Oct 08 '15 at 10:16
  • 1
    Possible duplicate of [Android: Cancel Async Task](http://stackoverflow.com/questions/6039158/android-cancel-async-task) – Sree Oct 08 '15 at 10:17
  • You can't cancel all instances WITHOUT a reference to EACH individual AsyncTask. Read docs on what cancel() does form AsyncTask. – JoxTraex Oct 08 '15 at 10:17
  • Actually there are different instances of the same asyncTask and i want to cancel all of them from the same event – Awadesh Oct 08 '15 at 10:17
  • @Sree its not the duplicate please try to understand this it is different case – Awadesh Oct 08 '15 at 10:19
  • @JoxTraex okk thanks , so i have to track all the instances of the asynctask – Awadesh Oct 08 '15 at 10:20

5 Answers5

3

You can't cancel all instances WITHOUT a reference to EACH individual AsyncTask.

Read docs on what cancel() does form AsyncTask.

Once you have a reference to each AsyncTask, then you can cancel each of them.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
2

use following code to cancel running asynctask

if(myAsyncTask.getStatus().equals(AsyncTask.Status.RUNNING))
  {
 myAsyncTask.cancel(true);
 }
sasikumar
  • 12,540
  • 3
  • 28
  • 48
2

I could solve it making a flag in OnStop()

1- Declare a global

    int flagOnStop=0;

2- Declare all global AsyncTask -->

    Async_Task asyncTask1,asyncTask2,asyncTaskn;

3- Before execute it, ask for flagOnStop -->

if(flagOnStop==0{
    asyncTask1=new Async_Task();
    asyncTask1.execute();

    asyncTask2=new Async_Task();
    asyncTask2.execute();

    asyncTaskn=new Async_Task();
    asyncTaskn.execute();

}

4- When your activity stop make the flag equals to one

@Override
public void onStop() {
    super.onStop();
    flagOnStop=1;
    asyncTask.cancel();
    asyncTask2.cancel();
    asyncTaskn.cancel();
}
0

Set a boolean flag in your activity (or wherever visible to the asynctasks) and inside the asynctask do:

while (flag) {
    your_logic;
}

When pressing back set the flag to false and every asynctask should stop itself - then you don't need a reference to the asyntasks themselves because they will do all the work.

Jon
  • 7,941
  • 9
  • 53
  • 105
0

FOUND THE SOLUTION: I added an action listener before uploadingDialog.show() like this:

uploadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
      public void onCancel(DialogInterface dialog) {
          myTask.cancel(true);
          //finish();
      }
});

That way when I press the back button, the above OnCancelListener cancels both dialog and task. Also you can add finish() if you want to finish the whole activity on back pressed. Remember to declare your async task as a variable like this:

MyAsyncTask myTask=null;

and execute your async task like this:

myTask = new MyAsyncTask();
myTask.execute();

Reference : Android: Cancel Async Task

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
  • This is not geared towards his question. He wasn't asking how do you cancel one AsyncTask, he was asking about how you cancel several. Please refer to my post. – JoxTraex Oct 09 '15 at 04:16