You are 100% on the right track; this calls for a service. As for what happens when your activity is no longer running -- well, let's consider that.
First, if your activity is simply no longer the current activity -- the user may have pressed Home, for example -- he may switch back to it. You'd like to have actually made some progress in the meantime -- especially if he left because he was tired of waiting for the transfer. You probably want to let the transfer continue until onDestroy() is called on your Activity, at which point you can stop the service. If your activity is truly gone -- well, would that information be useful if he started up your program again later? If so, you might want to consider allowing the transfer to finish, and caching the result. In this model, the activity's onDestroy() method leaves the service alone, and the service calls stopSelf() when it's done. Next time it's started for this purpose, it can check for the cached data and immediately supply that. If caching is inappropriate, and you want it to only run when the Activity is still alive, then use bindServce() instead of startService(), and unbind the service in the Activity's onDestroy(). The Service can track how many bindings are active, and exit early out of its loop reading the data. You couldn't do any better than this at stopping the activity even if you were doing it in the Activity anyway. On Apr 16, 9:57 pm, patbenatar <[email protected]> wrote: > Hey all- > > I've run into an interesting little issue.. My loader Activity fetches > data from a web API, starting an http request and waiting for the > result.. Now what if the user flips their phone mid-http request? The > Activity is destroyed and restarted, thus the http request restarts as > well. The fact that my Activity is being destroyed on orientation > change is all good by me, I understand the way Android works, I'm just > looking for a solution to avoid restarting the http request on every > orientation change.. > > I'm thinking the solution would be Services... I could run a Service > in the background to do the http request and when it finishes, call > back to my Activity and let it handle the data and finish up. The one > thing I'm a bit weirded out about is what if the user closes my app > before the Service's http request finishes? What will it do with the > data if the Activity that needs it is no longer running? > > Thoughts, feedback, ideas, etc would be great! > > Thanks, > Nick > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group > athttp://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

