Singleton object with context?

by Chuck Lega » Mon, 11 Oct 2010 02:44:49 GMT


Sponsored Links
 Hi,

I often run into the following dilemma: I need some singleton-like
object in my app (typically some central manager-like thing) and this
object needs a context for various things. This means that I either
have to pass a context to all methods needing one or have some sort of
init-pattern to the object. Neither is pretty I think. What is the
correct way of doing this? Or is this simply (even the definition of)
a Service?

Anyone have any thoughts on this?

/Chuck

--



Singleton object with context?

by Mark Murphy » Mon, 11 Oct 2010 03:20:25 GMT


 


You really really really need to be careful about putting a Context in
a static data member, as you can run into memory leaks if Android
wants to destroy that context later. I recommend that you either pass
in the Context as needed, convert it into a Service that your
activities bind to, or make it a custom Application object (which is a
Context, though you may still find times when you'll need an
Activity's context).

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

_Android Programming Tutorials_ Version 3.0.1 Available!

--


Sponsored Links


Singleton object with context?

by Zsolt Vasvari » Mon, 11 Oct 2010 08:40:21 GMT


 I always just bite the bullet and pass Context as an argument.  Yes,
it's a bit more typing, but it's safer and besides, that's how most of
the Android API's work.






--



Singleton object with context?

by Dimitris » Tue, 12 Oct 2010 02:01:54 GMT


 The context class is abstract. When you say context do you mean an
Activity context? I suggest you look at the Application class (see
getApplicationContext()) and pass that instead. It is much much safer
from memory leaks rather than passing the Activity around.

Try to also override the finalize() method of your activity and put a
log in there to ensure the activity is dead.






--



Singleton object with context?

by Mark Murphy » Tue, 12 Oct 2010 02:13:45 GMT


 


Bear in mind, though, that the Context returned by
getApplicationContext() is not going to be suitable in all cases. I
and others have run into problems with this related to UI operations.

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

Android Training...At Your Office:  http://commonsware.com/training 

--



Singleton object with context?

by Bret Foreman » Tue, 12 Oct 2010 02:30:24 GMT


 I second Mark on that. getApplicationContext is dangerous. The
Application context differs in many subtle ways from an Activity
context.

--



Singleton object with context?

by Dimitris » Tue, 12 Oct 2010 04:58:44 GMT


 Agreed. For UI related cases that you might want to use that Context,
the application one is not suited.

Use depending on what you are trying to do :)






--



Singleton object with context?

by Al » Tue, 12 Oct 2010 05:29:53 GMT


 Consider extending the Application class and using that to store
objects which are required throughout activities. As Dianne said, you
are less likely to leak a context if you use that.






--



Singleton object with context?

by Dianne Hackborn » Tue, 12 Oct 2010 09:02:05 GMT


 



The Context you have from extending Application is the same context you get
with Context.getApplicationContext().  I generally recommend that people use
singletons instead of extending Application, and the semantics of Context
here isn't different between the two.

-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--



Singleton object with context?

by Chuck Lega » Wed, 13 Oct 2010 02:37:23 GMT


 Thanks for great input!
I guess the Application object may be a simple way to store some
common data, but when implementing an Util/Mgr-like class with state
and context I think a separate class is to prefer.
Passing context with each call may be "clean", but does not cover all
cases. For instance, say I register a listener in the singleton. The
callbacks to the listener does not contain a context, and I am back at
square one again...

So, I guess init-ing the singleton with a context is the simplest way,
and making it a service the most robust/correct(?).

/Chuck






--



Singleton object with context?

by TreKing » Wed, 13 Oct 2010 03:04:26 GMT


 



What listeners and callbacks are we talking about? You may want to provide
more, um, context (har har) about how you're using a singleton, callbacks,
listeners, and the Context. There's probably a better way than initializing
the singleton with the Context object.

And making your singleton a Service may not necessarily be the most robust
/correct - it depends on what you're trying to achieve with this singleton.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

--



Singleton object with context?

by Chuck Lega » Wed, 13 Oct 2010 03:15:39 GMT


 Ok, I may be a bit vague, but say I want an utility class to do some
common stuff, including resources and shared prefs. Then I need a
context. If I put a SharedPreferenceListener into the class, I am lost
if I want to do something using a context when receiving the
callbacks.

This is how I think about it now: singleton object with init(ctx).
Init is called from App, i.e. with appctx.
What would typically this (app)context not be suitable for?

Sorry for deviating...

/Chuck






--



Singleton object with context?

by Zsolt Vasvari » Wed, 13 Oct 2010 06:31:48 GMT


 Most listeners/callbacks that I use are annonymous inner classes in
methods that already take a Context as a parameter.  In that case I
just make the parameter "final" and use it from the callback method
without it having to receive a Context directly.







--



Singleton object with context?

by Dianne Hackborn » Wed, 13 Oct 2010 06:56:54 GMT


 If you are loading resources, you should strongly consider making this a
"singleton per context".  The resources you get for different contexts can
vary due to themes and such.

If your singleton is doing things you know are global to the app -- for
example loading concrete resources that can't be changed by theme or
configuration, and loading shared preferences under an explicit name -- then
truly making it a singleton is fine.  Just have the call to get the
singleton take a Context, and when called the first time and constructing
the singleton use getApplicationContext() on that to keep the context you
will be holding in your singleton.  This is essentially the context for your
package, which is also your class loader, which you are singleton of.  There
is nothing funky or wrong with putting things together like that.

So doing a singleton this way is in my mind the simplest, and as long as you
know what you are doing and these are the semantics you want there is
nothing incorrect or not robust about it.






>



Singleton object with context?

by Chuck Lega » Thu, 14 Oct 2010 01:42:12 GMT


 Well put, as always. Thanks Dianne!
/Chuck






> >



Other Threads

1. SD Card format

SD card format required after upgrading 2.3.3 version of Android?

-- 

2. update screen frequently

Hi all, In my application I have list of multiple rows and columns.
The values of these rows and columns are coming from server and these
values are changes frequently. (near about after every second) Now I
want to show updated values each time without refreshing or pressing
back button. How to achieve this? Thanks in advance.

-- 

3. Designing a barcode app which doesnt sign your life away

4. How to point a marker on google map according to our choice.

5. [WTShare] Superbox

6. Configuration setting for peak performance of HTTP download

7. [Help] Ngga bisa download market