I'd like to use a static method to get a context. How do I do it
android android-context
First of all, the Android Manifest file is
<application android:name="com.xyz.MyApplication">
</application>
If it's like this
public class MyApplication extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
MyApplication.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApplication.context;
}
}
Do it like this. If you use MyApplication.getAppContext(), you can get it statically anywhere.
© 2024 OneMinuteCode. All rights reserved.