I’m using Apache Commons to make my HTTP calls, and in my activity I have a thread that makes the call. If a call is made by a device and is sitting there waiting to connect (imagine that the network is congested and the call is taking some time) and in the mean time, the user exits the app and the Activity is backgrounded. What happens to my call? For that matter (come to think of it) what happens to any thread that was started within the activity? If I have a thread that’s sitting there and doing its thing (whatever it is) and the activity is closed, do I have to handle killing the thread myself onPause()?
Edit…
So, given Ted Hopp’s answer, what is the right way to kill the Apache request? I’m looking through the Commons documentation and there are a bunch of methods that *look like they would do the trick, but since the doc is so lacking in details (why even bother with a doc if you’re not going to specify things?!) I’m wondering which of those methods I should call on my HttpConnection object before killing the thread that my connection is sitting in?
Closing an activity has no direct effect on the process in which the activity is hosted. All other threads continue running. You should override one or more of the life cycle methods (
onPause,onDestroy, etc.) to detect that the activity is finishing and interrupt or otherwise terminate running threads. The activity methodisFinishing()is useful for distinguishing between an activity being shut down and one that is being destroyed and restarted due to a configuration change.Your thread should be written with a method to cancel execution. With apache-commons in particular, that method should have two steps. First, you should call
abort()for the method that you passed to yourHttpClient.execute()method. Second, you should close the connection itself:Actually, if you do just the last step, that should actually suffice to throw an
IOExceptionin the method and stop everything.