Skip to content

Instantly share code, notes, and snippets.

@GrzegorzDyrda
Last active June 28, 2017 14:42
Show Gist options
  • Save GrzegorzDyrda/df55e464b76124f9d7083c922fbcada6 to your computer and use it in GitHub Desktop.
Save GrzegorzDyrda/df55e464b76124f9d7083c922fbcada6 to your computer and use it in GitHub Desktop.
Static way to obtain Application instance in Android!
/**
* 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