Depending on your specific needs, a service might make sense, but for
simple cases, it's not strictly necessary. You can instead use
Activity.onRetainNonConfigurationInstance(), which allows you to pass
live Java objects to the new Activity instance - i.e., for example an
AsyncTask object. Just make sure you don't create leaks by keeping
references to old instances of your Activity around.

I've been using a custom ASyncTask child class for a while that allows
me to "connect" to the task from a new activity instance, and have the
proper callbacks be executed even if the tasked finished in-between
activities being available:

http://github.com/miracle2k/android-autostarts/blob/master/src/com/elsdoerfer/android/autostarts/ActivityAsyncTask.java

Note that while the docs say that onRetainNonConfigurationInstance is
only for "optimization", it's not quite clear what that the official
contract is. Yes, it's possible that your activity needs to restore
itself without onRetainNonConfigurationInstance(). For example, if the
user leaves your application via HOME and then goes into the Settings
and changes the device language, your activity will be stopped and
restarted, presumably without onRetainNonConfigurationInstance() being
called.

However, it might be ok if in those cases your api request will indeed
need start again fresh, depending on it's duration, and maybe whether
it changes state on the server. But for your run of the mill
orientation change, onRetainNonConfigurationInstance() is apparently
being reliable called, and it has been suggested on this list a number
of times that it is intended to be used for objects that can't be
stored in a bundle, like for example sockets.

Michael

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to