On Friday, March 15, 2013 5:29:03 AM UTC+4, Lew wrote:
>
> Kristopher Micinski wrote:
>
>> I guess the bigger problem that in Android static data members cannot 
>> be statically checked to be "alive".  [...]
>>
>
if (gInstance != null) not working in Android for some reason?
 

>
>> Moreover, in Android it's a fact of life that your app will die and 
>> restart.  You can really only use statics for "caching" type purposes, 
>> but working with them in a safe way quickly becomes extremely 
>> complicated.  Instead of doing this, you can typically replace 
>> singletons with some Android specific utility (a Service or 
>> ContentProvider, say..) that allows you to implement the "singleton" 
>> type pattern. 
>>
>
How do you replace SharedPreferences or MimeMap or SqliteDatabase with a 
service?

How do you put ViewCompat.IMPL into a content provider?

https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/view/ViewCompat.java

( lines 339 - 355, horrible, absolutely horrible... ;P )

The code above shows one good reason to use singletons: to reduce memory 
allocations.

 

>
>> This really *is* a pretty frequent problem when people get UI elements 
>> stuck into static variables and then users rotate the screen :-) 
>>
>
It really *is* a pretty frequent problem when people smash their fingers 
with hammers.
 

>
> The biggest problem I have with singletons is that everyone for some 
> god-awful reason
> insists on lazily instantiating them. Why?
>

In the context of Android, that god-awful reason is often called "Context".

And speaking for myself, I've had a lot more trouble with Android specific 
issues (bugs and weird stuff in stock and manufacturer firmwares, 
inaccessible stuff in the framework) or with various other external factors 
affecting my apps, than with singletons in my code (or "for" loops, for 
that matter...)

So, don't really understand what all the bashing is about. It's a tool, use 
it correctly, and you'll be fine; use it wrong, and you could end up with a 
sore finger.

Just my 2 kopeyka's.

-- K
 

>
> Lazy instantiation is lazy. What's wrong with non-lazy instantiation? Then 
> you can use 
> a 'final' reference to the singleton instance that exists without effort 
> or overhead.
>
> -- 
> Lew
>  
>

-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to