For a first cut I would assume writing your activity to deal correctly with
asynchronously binding to the service and filling in its UI once it has a
connection.  This is basically the same as any other situation where you
need to populate your UI from some data source -- from a content provider,
data on the network, etc.  Your app *must* be able to deal with the
situation where the activity starts before the service does, so this case
must be working, and is sufficient to have a working app.

Sometimes in such situations people will have a common Activity base class
that takes care of binding to the service and such, so subclasses just need
to hook in to the point where the service is available.

The main issue you may have at this point is that your UI has some flicker,
where when it is first shown the data is empty and that gets filled in very
quickly after once the binding to the existing service is complete.

A possible trick you can use to deal with this is, if you know your service
is running in the same processes as your activities (this should be the
case), then in Service.onCreate() it can set a static variable containing
the service instance.  (And of course clear it in Service.onDestroy()).  Now
when you init the activity, after binding to the service you can check to
see if that variable is set and if so immediately populate your UI with the
currently running instance.  This is basically an extension on the local
service sample code in the docs.

On Thu, Apr 28, 2011 at 3:19 PM, Eric <[email protected]> wrote:

> My application is full of Activities that are absolutely dependent on
> a connection to a financial data server (trading system).  The
> Activity has the following requirements:
>
> - a user needs to be logged into the server in order to have
> permission to view data
> - the Activity has to query methods on the loggedOnUser to find
> trading permissions
> - the Activity has a large amount of UI view code that depends on
> extracting values from trading orders
>
> If the connection to the server, which I hope to implement in a
> Service, is down or not connected yet, what is the best practice for
> implementing the Activity, for login permission purposes, and also to
> avoid NullPointerExceptions when asking for data through methods that
> may return null?  And what is the best practice for publishing Service
> data to the Activity?
>
> I see a couple of solutions
>
> - write the Activity so that it starts up no matter what the status of
> the connection/Service, and in all of the Activity methods that deal
> with data to guard agains NullPointerExceptions (i.e. assume anyhing
> coming from the server could be null at any time
>
> - or, in the onCreate() of the Activity, check for the existence of a
> fully connected Service.  IF it's there, allow the Activity to start
> and assume that all data is non-null.  if the Service disconnects,
> finish() the Activity and go to an interim Activity saying,
> "reconnecting please wait...'  restart the Activity when Service is
> connected again.   IF the Service is NOT viable on Activity
> onCreate(), finish() the Activity in onCreate() and start the
> temporary "connecting please wait" Activity, and when Service is
> viable again, start the main Activity back up.
>
> --
> 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
>



-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

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

Reply via email to