Accessing Resources without a Context

后端 未结 5 1710
小鲜肉
小鲜肉 2020-12-03 04:31

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 everywh

相关标签:
5条回答
  • 2020-12-03 04:59

    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);
    
    0 讨论(0)
  • 2020-12-03 05:13

    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"
     ... >
    
    0 讨论(0)
  • 2020-12-03 05:15

    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.

    0 讨论(0)
  • 2020-12-03 05:18

    The stackoverflow answer to the question below shows how to use POJO to obtain a stream to a resource, if you supply its path. This might be useful in cases you need to select a specific resource from one of many.

    Is it possible to read a raw text file without Context reference in an Android library project

    0 讨论(0)
  • 2020-12-03 05:19

    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());
    
    0 讨论(0)
提交回复
热议问题