On Wed, Sep 28, 2011 at 10:17 AM, <Hera> <[email protected]> wrote: > I am developing an application compose by: 4 activities, one > application class (Global) and a remote service with provides the data > shown in the active activity (the same data is needed for all > activities).
You do not need a remote service. > The application class (Global) is where I define and interact with > objects that are used by all the activities. Moreover, as it is only > instantiated ones at the beginning of the application, I also do > things that must be done only once: opening a database at the > beginning (the same database is used by both, the remote process and > the activities). You do not need a remote process. > As the data returned by the remote service must be redirected to the > current activity, I bindService() and receive the data from the > service in the Global class. I start the service in the onCreate of > Global class in order to get the data as soon as possible. > > I thought that this Global class was only instantiated once during the > whole application (at the beginning) but when I bindService(), this > class is instantiated again. Therefore, the onCreate is again executed > and another process is started. That is because you are using a remote process when you do not need one. Get rid of the remote process, and your problem will be solved. > Can someone tell me the correct way no do this? Is It possible to > define a common class (with common objects) that is only instantiated > once and valid for both, activities and the remote service. Make it a local service. > Another thing that I would like to know is if is better to use > Singleton classes or application classes. A custom Application class does not necessarily give you anything over a singleton. The downside of an Application class is that there can only be one. In particular, I'd never create a library that depended upon a custom Application class, as that library would not be able to work in tandem with other such libraries. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://github.com/commonsguy http://commonsware.com/blog | http://twitter.com/commonsguy Android App Developer Books: http://commonsware.com/books -- 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

