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
  • 2,939
  • 10
  • 36
  • 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
  • 22,706
  • 6
  • 59
  • 82
0

add this method in asynctask:

@Override
    protected void onCancelled() {
        running = false;
    }
Sagar Maiyad
  • 12,482
  • 8
  • 60
  • 97