Office Hours — Today, June 7

Thursday, June 2

Jun 7
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:40 PM
Steve S.
has entered the room
Mark M.
hello, Steve!
how can I help you today?
Steve S.
Hi Mark!
I have a question regarding how to implement singletons in Android programs.
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.
What do you recommend.
Mark M.
use standard singletons
7:45 PM
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)
Steve S.
Ok.
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
but I wouldn't have Application *be* the singleton
Steve S.
Ok.
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
singletons for caches are fine
singletons for coordination among components are fine (e.g., event bus implementations)
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.
Mark M.
correct
7:50 PM
Steve S.
Ok. No more questions on that.
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.
Ok.
Thank you for your help!
Mark M.
you are very welcome
Steve S.
Have a great rest of the day.
Mark M.
you too!
Steve S.
has left the room
8:25 PM
Mark M.
turned off guest access

Thursday, June 2

 

Office Hours

People in this transcript

  • Mark Murphy
  • Steve S.