How can I get a resource content from a static context?


I want to read strings from an xml file before I do much of anything else like setText on widgets, so how can I do that without an activity object to call getResources() on?

3/23/2016 8:48:08 AM

Accepted Answer

  1. Create a subclass of Application, for instance public class App extends Application {
  2. Set the android:name attribute of your <application> tag in the AndroidManifest.xml to point to your new class, e.g. android:name=".App"
  3. In the onCreate() method of your app instance, save your context (e.g. this) to a static field named mContext 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;

    public void 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()).

12/14/2016 3:44:59 PM



You can use them everywhere in your application, even in static constants declarations! But for system resources only!

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow