2

Possible Duplicate:
onPostExecute on cancelled AsyncTask

Why does cancelling a AsyncTask still calls onPostExecute if the docs say it should call onCancelled instead. Is this a long persistent bug?

In my case it I cancel(true) the asynctask, it finishes the doInBackground and then invokes onPostExecutes. Finally it throws a

java.lang.InterruptedException

If this is intented behavour, can I get this Exception anyhow as an object?

Community
  • 1
  • 1
Arian
  • 3,183
  • 5
  • 30
  • 58
  • 1
    See http://stackoverflow.com/questions/3334858/onpostexecute-on-cancelled-asynctask – Gunnar Karlsson May 03 '12 at 10:07
  • 1
    actually asynk task dont get cancelled instantly . to make this really cancel you have to do some work and check it whether cancel is called or not using flag concept – Akram May 03 '12 at 10:07
  • yep gunnar this is what I'm experiencing. so this is a bug? – Arian May 03 '12 at 10:12

2 Answers2

1

After onPostExecute should be calls onCancelled

alezhka
  • 738
  • 2
  • 12
  • 29
1

If you read the documentation from the cancel() method you'll find this:

Attempts to cancel execution of this task. This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason.

calling cancel() will set isCancelled() to true. Are you periodically checking the return value of this method in your doInBackground?

   protected Object doInBackground(Object... x) {  
   while (/* condition */) { 
      // work...    
   if (isCancelled()) break; 
    }    
 return null;  } 

As for the exception, java.lang.InterruptedException , there could be multiple reasons.

My guess in your case is that you may be calling cancel() at a wrong place/time, and you may not be checking for isCancelled() periodically in doInBackground, so the task is completed suceessfully and onPostExecute() is called.

Akhil
  • 13,888
  • 7
  • 35
  • 39