On Dec 19, 8:18 pm, Y2i <[email protected]> wrote: > > Hm, so when do you need to just have code start running in your process > > first thing? Generally it needs to run for something else -- and that > > something else needs to make a call somewhere to get what it wants. Having > > that call Singleton.getInstance(context) (at which point you can do init > > for the first call) isn't any harder that > > ((Singleton)context.getApplication()), and it has some significant benefits: > > When some activity makes a first call to Singleton.getInstance(context), > the singleton presumably will keep a reference to the context, directly or > transitively; otherwise there would be no need to pass the context.
No, Diane is saying that you can say Singleton.getInstance(context.getApplication()) to pass the Application reference to the singleton and it will be used to initialize the singleton if not already. The situation where all of Diane's suggestions don't work is the case where you want said singleton to be accessible from an object that doesn't have access to any kind of Context. If the getter for the singleton requires an Application, then you have to have ensure that the singleton was created ahead of time and instead pass null or provide another getter that doesn't take an Application. Specifically, the case for me where Application.onCreate() is necessary is initializing singletons in this way. I have a lot of non- Android java code that hide the details of being run inside or outside of Android, and singletons are no exception. If running in Android, I want a version of my singleton to use a context. If not Android, a version with no context (something else). I need the Application.onCreate() entry point to establish this configuration for me or be forced to put that line of init code into every component's onCreate, which is horrible. Also, I like Application.onCreate to set up background threads to kick off work that need not impede the user's progress through the app. Again, horrible to do that in every component. Doug -- 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

