Last active
September 22, 2015 14:58
-
-
Save truedem/0688e11155462da00f1f to your computer and use it in GitHub Desktop.
Static context 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
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
staticMethod(); | |
} | |
public static void staticMethod() { | |
Context context = ApplicationBase.getContext(); | |
} |
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
<!-- variant: <application android:name=".ApplicationBase"> --> | |
<application android:name="com.abc.xyz.ApplicationBase"> | |
</application> |
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
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