Created
September 9, 2013 05:01
-
-
Save jpelgrim/6491673 to your computer and use it in GitHub Desktop.
Singleton with an init method taking an Android Application object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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