60

I want to stop a AsyncTask thread from another AsyncTask thread. I have tried like new AsyncTask.cancel(true) to stop the background process but it didn't stop.

Could any one help me on this?

Matt
  • 74,352
  • 26
  • 153
  • 180
user
  • 673
  • 1
  • 6
  • 10

6 Answers6

114

declare your asyncTask in your activity:

private YourAsyncTask mTask;

instantiate it like this:

mTask = new YourAsyncTask().execute();

kill/cancel it like this:

mTask.cancel(true);
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
  • 4
    after using like this YourAsynchTask mTask=new YourAsncTask(); mTask.execute(); worked. nice. – Senthil Nov 24 '12 at 12:12
  • 5
    It's kinda annoying though, that you cant really cancel if you got a single operation (like a database query) going on for instance. If the query takes a long time and is just one call, then it seems there is no way to cancel it :/ – AgentKnopf Dec 09 '14 at 10:01
  • 2
    If you are doing something inside the AsyncTask (in a loop) then you might want to check isCancelled() - refer https://stackoverflow.com/questions/10882543/how-to-completly-kill-remove-delete-stop-an-asynctask-in-android – amar Oct 13 '17 at 12:12
22

The reason why things aren't stopping for you is because the process (doInBackground()) runs until it is finished. Therefore you should check if the thread is cancelled or not before doing stuff:

if(!isCancelled()){
// Do your stuff
}

So basically, if the thread is not cancelled, do it, otherwise skip it :) Could be useful to check for this some times during your operation, especially before time taking stuff.

Also it could be useful to "clean up" alittle in

onCancelled();

Documentation for AsyncTask:

http://developer.android.com/reference/android/os/AsyncTask.html

Hope this helps!

DecodeGnome
  • 1,809
  • 1
  • 19
  • 36
  • if you pass true as a parameter, then it will stop on background task, so this would only be helpful if you pass false into `task.cancel(boolean)` – Dylan Vander Berg Aug 11 '15 at 17:39
  • @DylanVanderBerg That's not entirely true. If you do not design your code in a strictly linear fashion, knowing this is **extremely** helpful. While you may not check `isCancelled` in `doInBackground`, that is not the only place it is valid and `onCancelled` can be the difference between smooth operation and a memory error. – Abandoned Cart Mar 15 '20 at 14:33
12

You may also have to use it in onPause or onDestroy of Activity Life Cycle:

//you may call the cancel() method but if it is not handled in doInBackground() method
if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED)
    loginTask.cancel(true);

where loginTask is object of your AsyncTask

Thank you.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
7

You can't just kill asynctask immediately. In order it to stop you should first cancel it:

task.cancel(true);

and than in asynctask's doInBackground() method check if it's already cancelled:

isCancelled()

and if it is, stop executing it manually.

user2758776
  • 421
  • 1
  • 5
  • 15
1

I had a similar problem - essentially I was getting a NPE in an async task after the user had destroyed the fragment. 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;
    ...
}

Then, I check "if running" periodically in my async code. I have stress tested this and I am now unable to "break" my activity. This works perfectly and has the advantage of being simpler than some of the solutions I have seen on SO.

IanB
  • 3,489
  • 1
  • 20
  • 24
-3

u can check onCancelled() once then :

protected Object doInBackground(Object... x) {

while (/* condition */) {
  if (isCancelled()) break;
}
return null;

}