Android的辛格尔顿与全球背景 [英] Android Singleton with Global Context

查看:151
本文介绍了Android的辛格尔顿与全球背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每Android的文件就指出:

Per the Android Documentation it states:

通常没有需要继承的应用。在大多数情况下,   静态单身可以提供相同的功能在一个更模块化   方法。如果你单身需要一个全球范围内(例如注册   广播接收器),所述函数来检索它可以给出一个   Context,它内部使用 Context.getApplicationContext()时,   首先构建单。

There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

我如何去建立一个静态单,有全球背景下,使得它生存的运行活动在我的应用程序有什么变化?是否足以有一个静态的背景下,它引用了getApplicationContext()?

How do I go about creating a static singleton that has global context so that it survives the running activity changing in my app? Is it enough to have a static context which references the getApplicationContext()?

推荐答案

编辑/改进的答案:

edited/improved answer:

因为这个答案越来越流行的有点,我会提高我自己的答案与我一直在使用最近已经什么例如code(截至七月/ 2014年)。

because this answer is getting kinda-of popular, I'll improve my own answer with example code of what I've been using lately (as of Jul/2014).

首先,让应用程序保持对自身的引用。

Start by having the application keeping a reference to itself.

public class App extends Application {
   private static App instance;
   public static App get() { return instance; }

   @Override
   public void onCreate() {
      super.onCreate();
      instance = this;
   }
}

然后在需要访问上下文中的任何单我延迟加载使用双重检查同步这里解释的http://stackoverflow.com/a/11165926/906362

then on any singleton that needs access to the context I lazy load the singles in a thread safe manner using double check synchronization as explained here http://stackoverflow.com/a/11165926/906362

private static SingletonDemo instance;

public static SingletonDemo get() {
   if(instance == null) instance = getSync();
   return instance;
}

private static synchronized SingletonDemo getSync() {
   if(instance == null) instance = new SingletonDemo();
   return instance;
}

private SingletonDemo(){
   // here you can directly access the Application context calling
   App.get();
}

原来的答案:

什么文档建议是使用正常的单例模式

 public class SingletonDemo {
    private static SingletonDemo instance = null;

    private SingletonDemo() {       }

    public static SingletonDemo getInstance() {
            if (instance == null) {
                 instance = new SingletonDemo ();
            }
            return instance;
    }
}

和包括里面像这样的方法:

and include inside it a method like this:

 private Context context;
 init(Context context){
    this.context = context.getApplicationContext();
 }

和记住调用这个初始化的单例。

and remember to call this to initialise the singleton.

应用程序方法和辛格尔顿的做法,为什么单身更好的区别是更模块化的方式文档相同的功能

The difference between the Application approach and the Singleton approach and why the Singleton is better is on the documentation same functionality in a more modular way

这篇关于Android的辛格尔顿与全球背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆