getString Outside of a Context or Activity getString Outside of a Context or Activity android android

getString Outside of a Context or Activity


Yes, we can access resources without using `Context`

You can use:

Resources.getSystem().getString(android.R.string.somecommonstuff)

... everywhere in your application, even in static constants declarations.Unfortunately, it supports the system resources only.

For local resources use this solution. It is not trivial, but it works.


Unfortunately, the only way you can access any of the string resources is with a Context (i.e. an Activity or Service). What I've usually done in this case, is to simply require the caller to pass in the context.


In MyApplication, which extends Application:

public static Resources resources;

In MyApplication's onCreate:

resources = getResources();

Now you can use this field from anywhere in your application.