Last active
June 28, 2017 14:42
-
-
Save GrzegorzDyrda/df55e464b76124f9d7083c922fbcada6 to your computer and use it in GitHub Desktop.
Static way to obtain Application instance in Android!
This file contains hidden or 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
/** | |
* To make it work, just specify the fully-qualified name of this class as the "android:name" | |
* attribute in your AndroidManifest.xml's <application> tag. | |
*/ | |
public class Application extends android.app.Application { | |
private static Application ourInstance; | |
/** | |
* This gets called by the Android OS when the process for your application/package is created. | |
* <p> | |
* WARNING: Never call this one manually! | |
*/ | |
public Application() { | |
ourInstance = this; | |
} | |
/** | |
* Call this to obtain {@linkplain android.app.Application Application} instance anywhere in your code. | |
* | |
* @return Application instance | |
*/ | |
public static Application getInstance() { | |
return ourInstance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment