Santi Gil Santi Gil - 2 months ago 12
Android Question

Android. Get string resource for a non-activity class

I have a String resource:

<string name="myString">stringName</string>

How do I get the String from a non-activity-class?

I can't do

Ian Ian

You have to use reference to a Context to access resources. I'd recommend extending the Application class and creating an Application Singleton, then calling:


Or injecting it into your class of choice.

Problem with this approach is that it would make your class harder to test, since now it uses the Android context. I'd recommend passing a string as a dependency into the non-activity class of choice via the constructor or preferred method.

public MyClass(String string){