Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Resources without a Context

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.

like image 465
mnemy Avatar asked Apr 07 '11 00:04

mnemy


4 Answers

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.

like image 165
Gangnus Avatar answered Nov 19 '22 05:11

Gangnus


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"
 ... >
like image 22
SandWyrm Avatar answered Nov 19 '22 05:11

SandWyrm


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);
like image 2
dwp4ge Avatar answered Nov 19 '22 04:11

dwp4ge


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());
like image 1
sgarman Avatar answered Nov 19 '22 03:11

sgarman