I am downloading multiple images in an activity to populate a list view, all in seperate async tasks. In this activity the user can press a button to go to another page which doesn't require the images.
The issue I run into is that even on going to the next activity the async tasks are still running and the httpclient isn't released for the requests that need to be made in this next activity.
I tried using asynctask.cancel for all the tasks running, but that throws an interruptedIOException.
Is there any other graceful way of stopping the requests that are being made to free up the client?
Asked
Active
Viewed 1,160 times
0

user1132832
- 1
- 1
-
Implement threads and source solution from the following stackoverflow question. http://stackoverflow.com/questions/680180/whe re-to-stop-destroy-threads-in-android-service -class – wurde Jan 05 '12 at 18:44
1 Answers
1
Ideally, what you want to do in these kinds of situations is to wrap your HTTP requests in a loop that can be cancelled. Using this question as an example (full implementation there):
@Override
protected Void doInBackground(Void... params) {
while (running) {
// loop your HTTP requests here
}
return null;
}
When you trigger the onCancelled()
method (by calling cancel
), your doInBackground
method will terminate as soon as the current HTTP request is complete (and not before).
This is likely to be the most graceful solution you're going to get using AsyncTask
. Also, you might have to catch the interrupted exception somewhere in your AsyncTask
class, but this should be enough to get you going.

Community
- 1
- 1

Marvin Pinto
- 30,138
- 7
- 37
- 54