Avoid restarting http request on orientation change

by patbenatar » Sat, 17 Apr 2010 12:57:37 GMT


Sponsored Links
 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

--



Avoid restarting http request on orientation change

by Bob Kerns » Sat, 17 Apr 2010 14:47:30 GMT


 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.




>


Sponsored Links


Avoid restarting http request on orientation change

by patbenatar » Sat, 17 Apr 2010 18:46:12 GMT


 Awesome. Thanks Bob! This has been very informative. I will pursue
this route :)






> >



Avoid restarting http request on orientation change

by Dimitris » Sun, 18 Apr 2010 02:02:41 GMT


  would take into consideration that onDestroy() might not get called
always. Usually onPause() is the best place to store/cache data before
you are done because it is guaranteed to get called. Keep in mind not
to do "expensive" or time consuming operations onPause() because the
other activity that is trying to show won't show until your onPause()
is done.

I recommend reading the Android activity life cycle docs on the
android dev docs.

Enjoy :)

On Apr 17, 3:45am, patbenatar <patbena...@gmail.com> wrote:
> > >



Avoid restarting http request on orientation change

by Bob Kerns » Sun, 18 Apr 2010 05:54:00 GMT


 f this were a matter of saving the data, then onPause() is indeed the
place to do the work. However, it's not appropriate here.

This is not something you want to pause. You increase the chances of
connection failure, and you consume resources on the server. Don't do
that.

You also don't want to stop it there, and restart it on resume. That
just causes extra work, and minimizes your chance of success.

onDestroy() is the right place to actually destroy the background
activity here, if you want it to continue while the activity continues
to be "active". Yes, in some circumstances onDestroy() won't be
called, and the process will just be deleted. That will also nuke the
service, though, so that doesn't present a problem.

On Apr 17, 11:02am, Dimitris <dnkou...@gmail.com> wrote:
> > > >



Avoid restarting http request on orientation change

by patbenatar » Sun, 18 Apr 2010 10:00:05 GMT


 greed, Bob. I will pursue your above route :)

Dimitris- Thanks for the input. I have read the Activity lifecycle
docs and believe I have a pretty solid understanding of the Activity
lifecycle, this issue is however more related to the Service
lifecycle.


On Apr 17, 2:53pm, Bob Kerns <r...@acm.org> wrote:
> > > > >



Avoid restarting http request on orientation change

by Michael Elsd枚rfer » Sun, 18 Apr 2010 14:25:27 GMT


 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

--



Avoid restarting http request on orientation change

by patbenatar » Mon, 19 Apr 2010 16:22:19 GMT


 Michael-

An interesting approach indeed. Sounds a little hacky, though...
Especially if the docs vaguely advise against it as you mention. A
clever hack, though :)

-Nick





>



Other Threads

1. My game runs much slower if i DO specify *any* min SDK version in Manifest.xml

Did you declare support for varying screen sizes?

http://developer.android.com/guide/practices/screens_support.html

03.08.2010 17:19, mrqzzz :
...and if i don't specify any min SDK version, it runs smooth. I get average 30 fps vs. 12 fps. I am using Canvas to render and a JBox jar for physics. Tested and re-tested on a Motorola Milestone with Android 2.1 update 1. Using latest Eclipse ADT. Any tips? Thanks, Marcus.
-- Kostya Vasilev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com --

2. My game runs much slower if i DO specify *any* min SDK version in Manifest.xml

...and if i don't specify any min SDK version, it runs smooth.

I get average 30 fps vs. 12 fps.

I am using Canvas to render and a JBox jar for physics.

Tested and re-tested on a Motorola Milestone with Android 2.1 update
1.
Using latest Eclipse ADT.

Any tips?

Thanks,
Marcus.

-- 

3. No Gui / black screen

4. Specs for graphics

5. Compiling module on tablet Dell Streak

6. ELF permissions

7. want to unsubscribe from this group