Android: getString(R.string) in static method
Asked Answered
D

6

30

When programming for Android sometimes you have to use static methods. But when you try to access you resources in a static method with getString(R.string.text) you'll get an error. Making it static doesn't work.

Does anyone knows a good way around this? The resource files in Android are very helpful for creating things in different languages or making changes to a text.

Devereux answered 29/9, 2010 at 14:43 Comment(0)
S
31

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.

Selfsatisfaction answered 29/9, 2010 at 14:45 Comment(0)
S
21

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

Surly answered 4/2, 2014 at 7:53 Comment(4)
This is a nice solution only the downside is that you can only use system resources.Devereux
@serv-inc - That's not what the Resources.getSystem() documentation says.Inspiratory
@serv-inc - I mean that according to Google's reference documentation, it doesn't matter if you import yourpackage.R; when using Resources.getSystem().getStringArray(), because Resources.getSystem() returns "a global shared Resources object that provides access to only system resources". Which strongly suggests that @SarenInden is correct and that the follow-up comment will only send people down the wrong path by suggesting they try something that the official documentation explicitly says won't work.Inspiratory
@aroth: something along the lines of https://mcmap.net/q/468562/-android-getstring-r-string-in-static-method was meant, but also for subpackages. Thanks for the hint that it could mislead people.Abreu
A
9

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);
}
Allred answered 26/1, 2017 at 10:18 Comment(0)
M
7

Pass in a Context (i.e. Activity) instance as a parameter object to static method. Then invoke getString on the parameter.

Marivaux answered 29/9, 2010 at 14:46 Comment(4)
This CAN be a bad idea. If you're storing references to contexts you could cause a memory leakSeamount
What made you think that I suggest to store a Context reference?Marivaux
@Falmarri: you can always use getApplicationContext(), which would then valid the whole app lifetimeAbreu
^ wrong. getApplicationContext() will not work in a static method.Smokedry
M
0

The post below gives a tip for creating an Application class to save your current context. Your new Application class will then be accessible from any other static method.

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

Martelli answered 7/1, 2012 at 19:32 Comment(0)
S
-2

One way is you can pass context to your static method. check this out it definitely works

public class Sounds {

    public static MediaPlayer getSoundTouch(Context context){
        return MediaPlayer.create(context, R.raw.touch);

    }

    public static MediaPlayer getSoundLeak(Context context){
        return MediaPlayer.create(context, R.raw.leak);

    }

    public static MediaPlayer getSoundFinish(Context context){
        return MediaPlayer.create(context, R.raw.finish);

    }

 }
Surly answered 30/3, 2014 at 5:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.