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 n
Create in onCreate()
an instance of getApplicationContext()
(mContext
) then call MyApp.getContext()
from everywhere in your app and you will get your application context statically.
public class MyApp extends Application {
//private static MyApp instance;
private static Context mContext;
public static MyApp getInstance() {
return instance;
}
public static Context getContext() {
// return instance.getApplicationContext();
return mContext;
}
@Override
public void onCreate() {
super.onCreate();
// instance = this;
mContext = getApplicationContext();
}
}
Remember to declare into your AndroidManifest.xml
...
...
...