Skip to content Skip to sidebar Skip to footer

How To Use The Singleton Pattern In An Android Project?

I know about Singleton, but I can't use it in an Android project. I am a beginner in Android. Please tell me how & where we can use Singleton in an Android project for Large Da

Solution 1:

Singleton in Android is the same as Singleton in Java:

A basic Singleton class example:

publicclassAppManager
{
    privatestatic AppManager   _instance;

    privateAppManager()
    {

    }

    public synchronized static AppManager getInstance()
    {
        if (_instance == null)
        {
            _instance = new AppManager();
        }
        return _instance;
    }
}

Solution 2:

  1. For "large data" use a database. Android gives you SQlite.
  2. Of course you can use Singletons in Android. What makes you think you cannot?

For more info on the singleton pattern, read http://en.wikipedia.org/wiki/Singleton_pattern

For more info on SQLite in android read: http://developer.android.com/guide/topics/data/data-storage.html#db

Post a Comment for "How To Use The Singleton Pattern In An Android Project?"