2

I have been struggling with this issue for a long long time, actually I've checked a lot of post in stackoverflow talking about the same but nothing definitive.

How to implement the Singleton pattern to achieve data sharing between Android activities? I am talking between activities, not classes, which one is the right way?

This is all the info I found:

1- The ones who recommend the standard Singleton form, the one that you might implement in Java, C, etc, here you got an example:

http://es.wikipedia.org/wiki/Singleton

2- The ones that suggest to implement it in the OnCreate method, like this:

http://androidcookbook.com/Recipe.seam?recipeId=1218

3- The ones that uses the Application implementation approach (not so sure about this one):

Is it acceptable practice to use Singleton Objects to save state or share data between Activities?

4- The ones that uses the "singleTask" approach, defining it in the manifest:

http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

5- And more exoteric ways like this one (Actually this is not a Singleton I think):

http://www.jameselsey.co.uk/blogs/techblog/android-implementing-global-state-share-data-between-activities-and-across-your-application/

Suggestions? Comments? Examples?

Google Android people recommends it as one way to share complex information between activities, but no clue about the best approach in Android.

http://developer.android.com/guide/faq/framework.html#3

Please help me to clarify this.

Community
  • 1
  • 1
Jachu
  • 405
  • 5
  • 10
  • Is there an actual question, or is this just a list of ways you found to make a shared singleton? – Geobits Mar 01 '13 at 18:11
  • Both, which one is the rigth way I would say. – Jachu Mar 01 '13 at 18:15
  • The "right" way to do it is the one that works. – Geobits Mar 01 '13 at 18:23
  • 1
    Dude, last guy that asked this got his butt in a sling: http://stackoverflow.com/questions/14271691/android-singleton . There's some stuff that might be of interest here: http://portabledroid.wordpress.com/2012/05/04/singletons-in-android/ – G. Blake Meike Mar 01 '13 at 18:23
  • Very useful article, thanks, so at the end we can say that a Singleton in Android never guarantees a single instance of the class, right?. Does it means that implementing a Singleton in an Activity is a Kind of lazy workaround? – Jachu Mar 01 '13 at 18:50

1 Answers1

3

I've used both the Application to hold a "Singleton" instance as well as a static final variable. In the framework I work on, Transfuse, Singletons are scoped via the @Singleton annotation. These hold the given singleton in a static final map:

@Singleton
public class SingletonExample{
    ...
}

http://androidtransfuse.org/documentation.html#singleton

And this is the map that holds the given singleton instance:

public class ConcurrentDoubleLockingScope implements Scope {

    private final ConcurrentMap<Class, Object> singletonMap = new ConcurrentHashMap<Class, Object>();

    @Override
    public <T> T getScopedObject(Class<T> clazz, Provider<T> provider) {
        Object result = singletonMap.get(clazz);
        if (result == null) {
            Object value = provider.get();
            result = singletonMap.putIfAbsent(clazz, value);
            if (result == null) {
                result = value;
            }
        }

        return (T) result;
    }
}
John Ericksen
  • 10,995
  • 4
  • 45
  • 75