First, it's initialized on demand, which itself has two benefits: you're not wasting time doing something unnecessary; you're not doing a bunch of stuff at application startup time, so it's more responsive.

Second, it's cleaner (at least to my eyes). Compare:

DataCache dc = ((MyApplication)getApplicationContext()).getDataCache();

with

DataCache dc = DataCache.get(this);

-- Kostya

06.02.2011 3:12, AndroidDevTime пишет:
What is the advantage of using lazy initialize Java Singleton vs.
using subclass of application as the Singleton itself?  Since we
already have getApplication() available in most places ....

On Feb 5, 3:03 pm, Kostya Vasilyev<kmans...@gmail.com>  wrote:
You can use a lazy initialize Java Singleton, where you pass in a context in
case one is needed to recreate state.

If you need to keep a context reference, make sure to call
getApplicationContext and keep that instead, so you don't run into object
lifetime issues and leak memory (in case that Context is an Activity or a
Service, and it goes away before the process does).

The stuff about not doing much in the application's onCreate I take to be
more of an architectural suggestion than a real framework limitation, you
can probably do as much there as in any other callback (e.g activity
onCreate), subject to ANR timeouts.

--
Kostya Vasilyev --http://kmansoft.wordpress.com
06.02.2011 1:38 пользователь "AndroidDevTime"<androiddevd...@gmail.com>
написал:

I have an Widget Application that also has Activities, and a heavily
used Service full of functions. I would like to keep a global status
for the application. This is app/widget specific state like say
STARTED, LEVEL1, LEVEL2 etc, so since the status is referenced in the
widget, the service, and activities I thought a static variable in an
Application Subclass might be good. So my question is twofold.
1) Where should I keep such a state/status variable as a syncronized
static variable in an application subclass?
And
2) Its first initial value depends on reading some values by calling
registerReceiver(null, intent) passing null for the receiver in order
to obtain some system values. Where should I do this initialization.
The Application.onCreate() docs indicate that not much should happen
in this method. I am thinking in the Widget.update method can call
getApplication and perform the registerReceiver operation and then
update the static variable in the Applicaiton subclass. Is this the
right place to initialize the application state?
Thanks
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com>
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to