Accessing Resources without a Context

mnemy picture mnemy · Apr 7, 2011 · Viewed 37.8k times · Source

I'm trying to put configuration, such as URLs/etc, into a resource folder for a utility class to use. However, I don't want to pass the Context from the activities everywhere. I would like to be able to access a resource via a path name (seems like assets/ was designed for this use), without using a context to access the resource.

In this particular case, I want a singleton to use something in configuration when it's instantiated. It has no need for anything from resources besides that one time during instantiation. Therefore, having to pass in a Context every time getInstance() is called would be a complete waste.

Also, this is specific to the App's configuration, and should not be in stored in a shared system file or anything like that.

Answer

Gangnus picture Gangnus · Jan 7, 2012

Use

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

You can use it ABSOLUTELY EVERYWHERE in your application, even in static constants declaration! But for system resources only.

For local resources use that solution.