This works for me:
public class MyApplication extends Application {
private static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
}
public static Context getContext() {
return mContext;
}
}
You just need to call MyApplication.getContext()any part of your application.
I assume the application XML tag is in manifest.xml
<application
android:name=".MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name" >
You do not need to create any instance of the Application class, it will be created when the application starts, before anything.
source
share