On Mar 27, 8:05 am, TreKing <treking...@gmail.com> wrote:
> On Sun, Mar 27, 2011 at 2:25 AM, Doug <beafd...@gmail.com> wrote:
> > It's not pointless, and please stop telling people that.  :-)
>
> > You just have to know when to use it.  Use it when you need to store
> > a context in an object that lives longer than an instance of an activity or
> > service, but don't want to leak that activity or service.  The application
> > context is perfect for that.
>
> I suppose that is true. Though IMO a Context is not something that should be
> stored in an object that outlives an Activity or Service. If you need a
> Context, one is usually readily available wherever you happen to be and can
> be passed as a parameter.
>
> Do you have a good example of when storing a Context like this is a good
> idea? I'm just curious.

I do it in an app I'm developing that has a bunch of background
actions going on in another thread.  A service wasn't appropriate for
these actions, and they need a context in order to do things like
broadcast messages, access resources, etc.  So the class that
encapsulates these behaviors best uses the application context to do
its work.

Generally speaking, if you have to create a persistent singleton of
some sort (cache, state machine, whatever) and that singleton needs a
context, only the application context will do.

MOST of the time there is no need for the application context, but
this app I'm working on is exceptionally complex as far as apps go.

Sorry for hijacking the thread -- anyway! -- TreKing is correct, there
is no need for application context when creating a dialog and you're
begging for trouble when you use it for mundane UI stuff.  However, it
is a necessity in some uncommon cases.

Doug

-- 
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

Reply via email to