Skip to content

Instantly share code, notes, and snippets.

@chris-piekarski
Created May 15, 2015 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-piekarski/186b1154e7a2b76f2ced to your computer and use it in GitHub Desktop.
Save chris-piekarski/186b1154e7a2b76f2ced to your computer and use it in GitHub Desktop.
Static Application Context Method
In Android Manifest file declare following
<application android:name="com.xyz.MyApplication">
</application>
then write the class
public class MyApplication extends Application{
private static Context context;
public void onCreate(){
super.onCreate();
MyApplication.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApplication.context;
}
}
Now every where call MyApplication.getAppContext() to get your application context statically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment