0

I have called asynctask.cancel(true) in a button click after the async task starts.i am checking for iscancelled value in doinbackground and wrote condition accordingly.the conditions is

while(myProgress<phnno.size()){

                if (isCancelled()) {


                    break;
                }else{
                     myProgress++;
                        publishProgress();
                           SystemClock.sleep(100);
                }

               }

But still the asynchronous task is not cancelled.Could anyone give suggestions regarding this. Thanks in advance

hemanth kumar
  • 3,068
  • 10
  • 41
  • 64

2 Answers2

1

Guessing because I haven't seen all of your code.

Make sure you are calling cancel() on the same AsyncTask object. Don't create a new one.

Like so:

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

Later, when you want to cancel

myTask.cancel();
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
0

add this method in asynctask:

@Override
    protected void onCancelled() {
        running = false;
    }
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99