2

I'm currently using asyncTask() to do some background exchanging of bitmap images as my activity progresses, and all works just fine; until I end the activity where the task resides. The task's thread goes into "wait" status instead of being destroyed? I've cancelled, and checked the return value of .isCancelled() as well. This wouldn't really be a problem except when I restart my activity again from a MAIN activity it will actually make a new thread for the new asyncTask(); thus leaving the old one sitting there "waiting" in the background? Is this a bug, or am I simply using this feature incorrectly?

While-E
  • 1,527
  • 2
  • 20
  • 37

2 Answers2

3

AsyncTask uses a thread pool. It is normal for you to see 4/5 async tasks in your debug panel. Just make sure that your async tasks do not hold strong references to the activity (try to make those async tasks static inner classes (or event separate classes) and have them hold a WeakReference to the activity instead of a strong reference.

Community
  • 1
  • 1
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
  • So could this theoretically cause some sort of threading chaos of there's a thread already alive and waiting on a `Tid` when a new activity is created and it wants to use that `Tid`? – While-E Apr 21 '11 at 09:23
0

i think you should use static flag variable in your doInBackground function for terminating operation or loop. In that way you can achieve your task

Sunil Pandey
  • 7,042
  • 7
  • 35
  • 48