Friday, March 15, 2013

Re: [android-developers] Re: Can this variable become null?

Statics? The state of the singleton is instance, not class based. Besides of the singleton itself. But it's private. And you access it using

public static void getInstance() {
    if (instance == null) {
       intance = new Whatever();
    }
    return instance;
}

If the system destroys the app, it will be initialized in the next access - safe.

And the possibility that someone can do bad things with the singletons, like storing views or other things which can lead to memory leaks is not a reason to stop using them. You can do this with any class.


Am Freitag, 15. März 2013 00:45:49 UTC+1 schrieb Kristopher Micinski:
I guess the bigger problem that in Android static data members cannot
be statically checked to be "alive".  By this I mean: you should try
to get as much static checking as possible, and if you're using
statics you don't have any ability to properly check this.

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.

This really *is* a pretty frequent problem when people get UI elements
stuck into static variables and then users rotate the screen :-)

Kris

On Thu, Mar 14, 2013 at 7:11 PM, Mark Murphy <mmu...@commonsware.com> wrote:
> On Thu, Mar 14, 2013 at 7:00 PM, user123 <ivans...@gmail.com> wrote:
>> What is the problem with singleton?
>
> http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil
> http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons
>
> And, since they don't seem to emphasize the point quite enough for my
> taste: static data members are, by definition, memory leaks. How *bad*
> of a memory leak they are depends on what they are and how they are
> used.
>
> Like many programming techniques, singletons can be used as a scalpel
> or a sledgehammer. The general advice against singletons is because
> most people reading that advice are inexperienced and are likely to do
> damage with either a scalpel or a sledgehammer.
>
> On the whole, AFAICT, tolerance for singletons decreases with
> increased production Java development experience, based on the
> conversations that I have had on the topic over the past few years.
>
> --
> Mark Murphy (a Commons Guy)
> http://commonsware.com | http://github.com/commonsguy
> http://commonsware.com/blog | http://twitter.com/commonsguy
>
> _The Busy Coder's Guide to Android Development_ Version 4.6 Available!
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-d...@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@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+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
--
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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment