Mark M. |
how can I help you today?
|
Tom |
Yes, I have a question...
|
Tom |
I have a variety of classes for which the member
functions requires a Context, or some other system handle (e.g. a
reference to TelephonyManager). I'd like to pass the handle into an
instance just once and have it hold that handle through its whole
lifetime (which is often the lifetime of the app) to avoid having to
keep passing the handle to member functions. Somehow though, I feel
like it is a bad idea for my class instance to hold a system handle for
so long, so I don't do it. Can you comment on this?
|
Mark M. |
personally, I don't do this
|
Mark M. |
I try to follow the approaches used by the Android SDK
|
Mark M. |
and they take the Context as a parameter on methods when they need it
|
Mark M. |
that being said, an Application object is a safe Context to hold onto for a long time, at least in terms of memory leaks
|
Mark M. |
you get this via getApplicationContext() from some other Context
|
Mark M. |
so, for example, in WakefulIntentService, I need to hold a WakeLock for the life of a process
|
Mark M. |
I create that WakeLock using the Application object, just to be safe, so I don't introduce a memory leak
|
Tom |
Okay, that sounds like a good idea. I was kinda
hoping you would say it is safe to hold anything - this would make my
function signitures cleaner - but I suspected that it would be a bad
idea.
|
Tom |
Also, the way I'm doing it now I feel that I must
be careful to be consistent with my system handles, e.g. I try to make
sure that I use the same context for de-registering an event as I used
to register it.
|
Tom |
Let me take this opportunity to say that I enjoy
your editorials (which I catch in Google Reader), but I think it would
be nice (for argumentative readers) if there way a way to respond, e.g.
blog comments.
|
Mark M. |
yeah, well, I specifically decided against that, to help keep my blood pressure in check... :-)
|
Mark M. |
it's possible I'll tie in DISQUS or something someday
|
Tom |
Yeah, I know what you mean. Anyway, thanks - that's all for today.
|