tags:

views:

185

answers:

2

Is there any reason not to use the Application class to share variables across Activities? For instance a handle to the DB or a single HttpClient.

+1  A: 

According to the official documentation "you can use it to maintain global application state". IMO you wouldn't want to hold onto to too many heavy objects, globally. Ofcourse, "too many" is too fuzzy:) There are other ways of sharing (persistent) data: Preferences, database, files. You might want to check whether any of these are a better fit for your problem.

The other thing that you need to understand about when using Application object is its Life cycle. Unfortunately, discussion of that can be very subjective but fortunately, such as discussion has already been done

Good luck!

Samuh
+2  A: 

The Application object is meant for maintaining Application state rather than resources. Personally, I would initiate DB connections or HTTP clients in each Activity that needs them rather than globally in an Application object.

I don't think there's a huge overhead for creating such objects and it means that when an Activity is stopped those resources can be freed up. If you create them in the Application object they will always be instantiated, using up memory, even if the current Activity doesn't need them.

Dave Webb