Office Hours — Today, July 10

Thursday, July 5

Jul 10
7:20 PM
Mark M.
has entered the room
7:25 PM
Mark M.
turned on guest access
7:40 PM
Tom
has entered the room
Tom
Hi Mark.
Mark M.
howdy, Tom!
how can I help you today?
Tom
Yes, I have a question...
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
I try to follow the approaches used by the Android SDK
and they take the Context as a parameter on methods when they need it
7:45 PM
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
you get this via getApplicationContext() from some other Context
so, for example, in WakefulIntentService, I need to hold a WakeLock for the life of a process
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.
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.
7:50 PM
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... :-)
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.
Mark M.
OK
Tom
has left the room
8:30 PM
Mark M.
turned off guest access

Thursday, July 5

 

Office Hours

People in this transcript

  • Mark Murphy
  • Tom