Created
May 15, 2015 19:16
-
-
Save chris-piekarski/186b1154e7a2b76f2ced to your computer and use it in GitHub Desktop.
Static Application Context Method
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
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