On Sun, Mar 27, 2011 at 12:30 PM, Kostya Vasilyev <[email protected]> wrote:
> 27.03.2011 20:17, Mark Murphy пишет:
>>
>> Singletons, manager, or utility classes should not be caching a
>> Context.
>
> They can and should if needed or wanted. However ...
>
>>  At most, they should be caching an Application, to prevent
>> people from accidentally caching short-lived Contexts (e.g., Activity,
>> Service).
>
> ... I'd further qualify that as "... a cached Context (if needed) should be
> an Application object reference, rather than a local, short-lived Context".

My point was that you should not have:

class MySingleton {
    Context thisIsAReallyBadIdea;

    MySingleton(Context ctxt) {
        thisIsAReallyBadIdea=ctxt;
    }

    // rest of class here
}

Instead, you should have:

class MySingleton {
    Application thisIsMildlyDisconcertingButProbablyOK;

    MySingleton(Application ctxt) {
        thisIsMildlyDisconcertingButProbablyOK=ctxt;
    }

    // rest of class here
}

In other words, use Java's type safety to ensure that you are caching
an object with the appropriate lifetime, and that you are using it as
the object with the appropriate lifetime.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in Oslo: http://bit.ly/fjBo24

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

Reply via email to