2

I've run into a case where my activity was paused, and from the onPause() method I canceled my AsyncTask. However, my logs show the following events in chronological order:

  1. OnPause called
  2. AsyncTask is canceled from OnPause. cancel(false) returns true indicating the task was successfully cancelled.
  3. onPostExecute is called, despite the contract that it won't ever be called if cancelled.

Has anyone seen this? Are LogCat messages guaranteed to be presented in accurate order?

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246

1 Answers1

2

As far as I've experienced LogCat messages come in correct order.

The issue might be that, your AsyncTask couldn't get cancelled. Calling Cancel() on AsyncTask is not a guaranttied way to cancel it. You may use the parameter boolean mayInterruptIfRunning to true. However even after setting it to true, it never worked for me.

You can check in onCancelled() method if the AsyncTask got cancelled or not. If the control reaches to this method, it means it got cancelled.
Have a look at answers in this and this questions.

Community
  • 1
  • 1
0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
  • See my edits. Even when the return value from `cancel(false)` is true, I'm seeing `onPostExecute` called. – Jeff Axelrod Aug 08 '12 at 18:01
  • "If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task" – 0xC0DED00D Aug 09 '12 at 05:35
  • 1
    So if you are calling cancel(false), and the task already started, it will not get stopped. However the returned value of cancel() has no significance in this case, despite what they wrote in the docs, it never worked for me too. Instead you should try with cancel(true). – 0xC0DED00D Aug 09 '12 at 05:36