0

I'd like to know if anyone has ever managed to terminate an Async task? Cancel doesn't work, for some reason, and every time you restart the Activity a new task is started, the previous ones still remain.

Kevin
  • 1,200
  • 1
  • 11
  • 19

1 Answers1

0

You shall not use AsyncTask for this purpose - see this related question which also provides a solution, but not the most ideal.

Community
  • 1
  • 1
Wroclai
  • 26,835
  • 7
  • 76
  • 67
  • I read the other question and I still don't understand why cancel doesn't stop the task. Surely it's just another type of Thread - and you can stop those without a problem. – Kevin Feb 26 '11 at 07:45
  • @Kevin: In that question - cancel does help. – Wroclai Feb 26 '11 at 11:48
  • Not according to my tests. Every time I the Activity executes I get extra Async tasks building up. Cancel doesn't kill the Async task. – Kevin Feb 28 '11 at 21:07
  • @Kevin: Cancelling threads in `Java` has been a problem for years. You should consider to redesign your application so it doesn't depends on if your `Thread` or `AsyncTask` has been canceled. – Wroclai Feb 28 '11 at 21:12
  • As I said before, I am not clear why stopping a thread is a problem. You can cancel a regular Runnable thread (just let the run() method finish) and it appears to me the same principle applies to an Async task. So why isn't the task removed when the onPostExecute() method finishes? – Kevin Feb 28 '11 at 21:33
  • When your `AsyncTask` is finished with `onPostExecute`, then it is removed. – Wroclai Feb 28 '11 at 21:44