如何在Android项目中使用Singleton模式?

Dee*_*ary 2 java singleton android

我知道Singleton,但我无法在Android项目中使用它.我是Android的初学者.请告诉我在大型数据的Android项目中如何以及在哪里使用Singleton.我用它来做简单的价值观.

Giv*_*ivi 41

Android中的Singleton与Java中的Singleton相同:

一个基本的Singleton类示例:

public class AppManager
{
    private static AppManager   _instance;

    private AppManager()
    {

    }

    public synchronized static AppManager getInstance()
    {
        if (_instance == null)
        {
            _instance = new AppManager();
        }
        return _instance;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 实际上,它是一个懒惰的单身人士,需要说明,它是_not_ Threadsafe! (7认同)