Android: getString(R.string) in static method

One way or another, you'll need a Context for that... For static methods this probably means you need to pass along a Context when calling them.


You could use Resources.getSystem().getStringArray(android.R.array.done);


This is how I access resources from inside static methods. Maybe not ideal, but.

First, I extend Application and set some public static field(s), and create a method to initialise them:

public class MyApp extends Application {

  // static resources
  public static String APP_NAME;

  public static void initResources(Context context) {
    APP_NAME = context.getResources().getString(R.string.app_name);
  }
}

And in my manifest I register the extended Application:

<application 
  android:name=".MyApp"/>

In my starter activity (MainActivity), I make a call to initialise the static resources:

@Override
protected void onCreate(Bundle savedInstanceState) {
  MyApp.initResources(this);   
}

Then anywhere in your project, after MainActivity.onCreate(Bundle b) has run, you can call static methods that access your specified static resources:

public static void printAppName() {
  Log.w("tag", "my app name: " + MyApp.APP_NAME);
}