The following schema has been touted as the way to get application context from anywhere within my android app. But sometimes doing MyApp.getContext()
returns null. I tried changing the schema by removing static
from getContext()
so that I would do MyApp.getInstance().getContext()
. It still returns null. How do I fix this? How do I get my application's context from anywhere within my app?
public class MyApp extends Application {
private static MyApp instance;
public static MyApp getInstance() {
return instance;
}
public static Context getContext() {
return instance.getApplicationContext();
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
}