Skip to content

Instantly share code, notes, and snippets.

@truedem
Last active September 22, 2015 14:58
Show Gist options
  • Save truedem/0688e11155462da00f1f to your computer and use it in GitHub Desktop.
Save truedem/0688e11155462da00f1f to your computer and use it in GitHub Desktop.
Static context in Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
staticMethod();
}
public static void staticMethod() {
Context context = ApplicationBase.getContext();
}
<!-- variant: <application android:name=".ApplicationBase"> -->
<application android:name="com.abc.xyz.ApplicationBase">
</application>
public class ApplicationBase extends Application {
private static Application application;
private static Context context;
public void onCreate() {
super.onCreate();
ApplicationBase.context = getApplicationContext();
}
public static Context getContext() {
return ApplicationBase.context;
}
public static Application getApplication() {
return application;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment