Set a boolean flag in onPause()/onStop()/onDestroy() (whichever makes sense), and have onPostExecute() examine the flag.
On Mon, Jul 26, 2010 at 11:31 PM, Eric Mill <[email protected]> wrote: > As far as I can tell, AsyncTask's cancel method doesn't work as > advertised. I can't tell whether this is a bug, or me not > understanding AsyncTask's cancel() method. I'm asking here instead of > StackOverflow because I suspect it's a bug. > > I understand from past discussions here that cancel() isn't meant to > actually kill a thread (though the description of the > mayInterruptIfRunning flag sure implies it is). But I'm also checking > for isCancelled() (which is returning false), and trying to use the > onCancelled callback (which isn't getting called), to no avail. > > My situation is that I have a (short-running, one small network call) > AsyncTask that begins in my Activity's onCreate. If the user backs out > of the screen before it finishes though, I need to either cancel the > AsyncTask entirely, or at least make sure its onPostExecute doesn't do > any meaningful work. > > In my activity's onDestroy handler, I call cancel() on the task, and > then inside the task I have an onCancelled handler that sets a > boolean. In my onPostExecute handler, I check isCancelled(), and I > also check to see whether the boolean has been set from onCancelled, > and neither are. > > I'm using the debugger, and the order of operations is: > > 1) onDestroy(), calls task.cancel() > 2) task's onPostExecute runs, isCancelled() returns false, so I have > no conditional to stop the flow > > and that's it. onCancelled never runs. > > What am I doing wrong here? I can't find any way to tell my task not > to run its work in onPostExecute. > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- Mark Murphy (a Commons Guy) http://commonsware.com | http://github.com/commonsguy http://commonsware.com/blog | http://twitter.com/commonsguy _Android Programming Tutorials_ Version 2.9 Available! -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

