While-E
While-E

Reputation: 1555

Android: Properly destroying asyncTask?

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?

Upvotes: 2

Views: 2828

Answers (2)

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28551

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.

Upvotes: 3

Sunil Pandey
Sunil Pandey

Reputation: 7102

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

Upvotes: 0

Related Questions