views:

355

answers:

1

Is there any fundamental difference in binding a service to an android.app.Activity vs binding it to an android.app.Application. I want to bind the service to an Application because I want to keep some global state/data in the Application instead of duplicating it in all my activities.

Thanks.

A: 

No. There is no fundamental difference.

That said, subclassing android.app.Application is a very good place to store global/state data. There is only one instance and everything that derives from Context has access to it.

I'm also sure that binding a service to an application will result in some odd lifetimes if you aren't careful. What I mean is that even though your app is out of sight and has no activities alive, your application could still exist because your service still exists. Your service still exists because your application still exists. You would have to manually shut down the service based on some event other than onDestroy.

Jere.Jones
Thanks. So, there is no clean way of shutting down the service in this case.
Soumya Simanta
I gotta ask, why do you want a service if you are going to store the data in the Application object? All of your activities have access to the app object via getApplication().
Jere.Jones
The service is going to perform all the I/O. I'm trying to implement something like http://stackoverflow.com/questions/3141632/android-service-interacting-with-multiple-activitiesMaybe I can store this global data/state in my service (?). Don't know if that's good design or bad design because this is the first time I'm working with multiple activities and services.
Soumya Simanta