How can I get a resource content from a static context?
- Create a subclass of
Application
, for instancepublic class App extends Application {
- Set the
android:name
attribute of your<application>
tag in theAndroidManifest.xml
to point to your new class, e.g.android:name=".App"
- In the
onCreate()
method of your app instance, save your context (e.g.this
) to a static field namedmContext
and create a static method that returns this field, e.g.getContext()
:
This is how it should look:
public class App extends Application{
private static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
}
public static Context getContext(){
return mContext;
}
}
Now you can use: App.getContext()
whenever you want to get a context, and then getResources()
(or App.getContext().getResources()
).
For system resources only!
Use
Resources.getSystem().getString(android.R.string.cancel)
You can use them everywhere in your application, even in static constants declarations!