Android and Singleton Classes

Recently in my Android programming escapades I found something interesting and bothersome in an API I had written. I had created a singleton class for the main renderer of my graphics API. With the first app I was making I had no problem, and it in fact was quite helpful and easy to write, after all each app using this API would only have access to one renderer.

The problem arose out of some strangeness with the Android VM. It turns out that static fields of a class can but not necessarily cross from one app to another, effectively sharing one singleton across more than one activity. This might not sound too bad but it can be a problem if you use an API in multiple apps and they happen to run on the same device. This can result in undesirable effects.

So I removed the singleton property from the renderer object, and thus had to pass it around in method parameters like a cheap whore. It’s not an ideal solution but it worked, and if you believe some people on the internet it’s probably a better design. Whilst there was minimal impact on the API, an app I was writing needed to be hacked a little to make it work properly. The funny thing is if there was a separate JVM or classloader for every instance of all apps it would have been fine to have the renderer as a singleton and it would work.

So why are singletons a problem on Android? It seems that there is a single JVM running all the Android apps together. This means that static variables (including singleton classes) can end up hanging around long after an activity ends. If a class is shared with another app they can end up using the same singleton class. The other half of the problem is the Android OS will sometimes destroy an app or use a different class loader for a new activity thus making the static/singleton unreliable for any kind of storage or communications across activities or apps.

So it’s best to avoid the use of singletons on Android.

There seems to have been some debate about singletons and their use. Whilst it is true that many programmers over use the pattern and they can be a problem, they do have a place in a developers toolbox. There are common interfaces such as OpenGL and others which cannot be accessed in an easily nice OO way.

OpenGL in particular is a good example, it is not thread safe and basically requires all rendering be done in a single thread. It also is not Object oriented and as such there is only one instance of it per application at best. Because of its nature it is essentially an interface to the hardware acceleration of a machine, which is better suited to a singleton/procedural style interface.

The thing with OO design is it isn’t suited 100% to all programming problems. There are in fact many instances where using an OO design is a bad idea, such as embedded systems and applications that require high performance. The trouble is all the OO loveliness comes at a performance and memory size cost, that luckily isn’t a problem for most applications. This leads to the situation where something OO needs to interface to something that isn’t, this is where singletons are a useful construct.

Something my Dad always said is “a bad tradesmen blames the tools”. It seems there are many people out there blaming the singleton as if it is at fault for being used incorrectly. Sure there may be many cases of people using it poorly, but that just means they are poor designers/coders. The singleton pattern is just a tool, used appropriately it can make your coding life easier, used badly it can make your life harder. This is true for all the basic design patterns, it’s up to the designers and coders how good their designs and code are.

1 Response to “Android and Singleton Classes”

  1. 1 androidev
    July 21, 2014 at 6:07 am

    I like looking through an article that will make people think.
    Also, many thanks for allowing me to comment!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Mister G Kids

A daily comic about real stuff little kids say in school. By Matt Gajdoš

Random Battles: my life long level grind

completing every RPG, ever.

Gough's Tech Zone

Reversing the mindless enslavement of humans by technology.

Retrocosm's Vintage Computing, Tech & Scale RC Blog

Random mutterings on retro computing, old technology, some new, plus radio controlled scale modelling.


retro computing and gaming plus a little more

Retrocomputing with 90's SPARC

21st-Century computing, the hard way

%d bloggers like this: