Accessing Resources without a Context

41,904

Solution 1

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.

Solution 2

You could extend the main application class and provide universal helpers there to access resources. This relieves the need for context as the application would provide the context instead of the caller. The application class is singleton style and should always be available while any portion of your application is running (including services).

public class MyApplication extends Application {
 protected static MyApplication instance;

 @Override
 public void onCreate() {
  super.onCreate();
  instance = this;
 }

 public static Resources getResources() {
  return instance.getResources();
 }
}

This provides you access to:

MyApplication.getResources()....

Be sure to declare your custom application in your manifest to gain access to this. Assuming your custom application is in the root of your application's namespace:

<application
 android:name=".MyApplication"
 ... >

Solution 3

I would recommend doing the following: Rather than passing context everywhere make your activity class a singleton class with a public function that returns the context:

private static ActivityMain instance;

Initialize inside onCreate() before super.onCreate():

instance = this;

Then add these public functions to your activity:

/** Get singleton instance of activity **/
public static ActivityMain getInstance() {
    return instance;
}

/** Returns context of this activity **/
public static Context getContext(){
    return instance.getApplicationContext();
}

Now you can use the following anywhere in any class:

Context context = AntiMorphActivity.getContext();
String packageName = context.getPackageName();
int id = context.getResources().getIdentifier("web_page", "raw", packageName);

Solution 4

Unfortunately I don't think there is a real way around this. I lay mine out something like this, and also pass in the getApplicationContext() instead of the activity context.

public static AppController getAppController(Context context){
    if(self == null) {
        //Create instance
        self = new AppController();
    }

    return self;
}

And then:

appController = AppController.getAppController(getApplicationContext());
Share:
41,904
mnemy
Author by

mnemy

Updated on February 14, 2020

Comments

  • mnemy
    mnemy about 4 years

    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.