Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Singleton with Global Context

Per the Android Documentation it states:

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.

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()?

like image 766
Derek Gebhard Avatar asked Dec 27 '12 15:12

Derek Gebhard


People also ask

How to get Context in singleton class android?

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.

Why singleton?

A singleton should be used when managing access to a resource which is shared by the entire application, and it would be destructive to potentially have multiple instances of the same class. Making sure that access to shared resources thread safe is one very good example of where this kind of pattern can be vital.

What is a singleton in programming?

What Does Singleton Mean? A singleton is a class that allows only a single instance of itself to be created and gives access to that created instance. It contains static variables that can accommodate unique and private instances of itself.


1 Answers

Another edit to the question (2016)

Lately (as of 2016 and onward) what I've been doing, and would be my suggestion for any developer, is:

Just use Dagger 2. Wherever you need a Context you do:

@Inject Context context; 

and that's it. While at it, inject all the other stuff that would be a singleton.

Edited/improved answer (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;    } } 

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 https://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(); } 

Original answer

what the documentation is suggesting is to use a normal singleton pattern

 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

like image 143
Budius Avatar answered Oct 18 '22 21:10

Budius