Mark M. |
how can I help you today?
|
Steve S. |
I have a question regarding how to implement singletons in Android programs.
|
Steve S. |
There seems to be a debate about whether that should be done by extending Application or, instead, by using a standard (non-Android-specific) singleton.
|
Steve S. |
Ok. So there's no issue about class loaders then.
|
Mark M. |
nothing beyond the Android normal (e.g., multidex shenanigans, if you have to go through those)
|
Mark M. |
now, if the singleton needs a Context, and it is OK to initialize the singleton on the main application thread, doing that initialization from Application#onCreate() is reasonable
|
Mark M. |
but I wouldn't have Application *be* the singleton
|
Mark M. |
just bear in mind that the more work you do on process startup, the slower that startup becomes, so the less you do in Application#onCreate() on the main application thread, the better off you are, all else being equal
|
Steve S. |
Ok. How long will static fields in the singleton retain their values after they have been initialized?
|
Mark M. |
until the process is terminated or you change the value
|
Mark M. |
singletons for caches are fine
|
Mark M. |
singletons for coordination among components are fine (e.g., event bus implementations)
|
Mark M. |
singletons are not for long-term data storage
|
Steve S. |
Ok, great. So the process might be terminated due to lifecycle events, and the fields would need to be reinitialized then.
|
Steve S. |
Ok. No more questions on that.
|
Steve S. |
On a different topic, I'm wonder what you would recommend in terms of formatting phone numbers. It seems like there have been some recent changes in the libraries provided in the SDK.
|
Mark M. |
I have not looked into formatting phone numbers, sorry
|
Steve S. |
Have a great rest of the day.
|