How to use the Singleton pattern in an Android project? [closed]

依然范特西╮ 提交于 2020-02-09 17:14:14

问题


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 Data. I have used it for simple values.


回答1:


Singleton in Android is the same as Singleton in Java:

A basic Singleton class example:

public class AppManager
{
    private static AppManager   _instance;

    private AppManager()
    {

    }

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



回答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



来源:https://stackoverflow.com/questions/12585720/how-to-use-the-singleton-pattern-in-an-android-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!