Skip to content

Instantly share code, notes, and snippets.

@jpelgrim
Created September 9, 2013 05:01
Show Gist options
  • Save jpelgrim/6491673 to your computer and use it in GitHub Desktop.
Save jpelgrim/6491673 to your computer and use it in GitHub Desktop.
Singleton with an init method taking an Android Application object
public class MySingleton {
static final Object mLock = new Object();
static MySingleton mInstance;
private Context mContext;
// Private constructor prevents instantiation from other classes
private MySingleton() { }
static MySingleton getInstance() {
synchronized (mLock) {
if (mInstance == null) {
mInstance = new MySingleton();
}
}
return mInstance;
}
public void init(Application app) {
if (mContext == null) mContext = app;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment