22

I have a class that needs to obtain a reference to it's application's AssetManager. This class does not extend any sort of android UI class, so it doesn't have a getContext() method, or anything similar. Is there some sort of static Context.getCurrentApplicationContext() type of method?

To clarify: my class is intended to be used like a library, for other applications. It has no associated AndroidManifest.xml or control over the context which is calling it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ab11
  • 19,770
  • 42
  • 120
  • 207

2 Answers2

27
  1. Create a subclass of Application, for instance public class App extends Application {
  2. Set the android:name attribute of your <application> tag in the AndroidManifest.xml to point to your new class, e.g. android:name=".App"
  3. In the onCreate() method of your app instance, save your context (e.g. this) to a static field named app and create a static method that returns this field, e.g. getApp():

This is how it should look:

public class App extends Application{

    private static Context mContext;

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

    public static Context getContext(){
        return mContext;
    }
}

Now you can use: App.getContext() whenever you want to get a context, and then getAssetManager() (or App.getContext().getAssetManager()).

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • 1
    To clarify, my class is intended to be used like a library, for other applications. It has no associated AndroidManifest.xml. – ab11 Dec 10 '10 at 17:42
  • 1
    In that case, create the asset manager in the application and pass it to the library (through a method or constructor). – Cristian Dec 10 '10 at 18:33
  • 3
    Seeing as how I intend for the library to be used by other developers in their applications, I was hoping there would be an alternative to requiring them to manually set the asset manager. – ab11 Dec 10 '10 at 19:14
7

I am not sure of the best answer to the OP question. However, I do know that you have to be very careful when using a static context as suggested in Android developer resources:

In the onCreate() method of your app instance, save your context (e.g. this) to a static field named app and create a static method that returns this field, e.g. getApp():

Using static contexts can leak to leaked memory issues, especially if the static context is used for references to views.

ib.
  • 27,830
  • 11
  • 80
  • 100
gymshoe
  • 7,495
  • 5
  • 20
  • 21
  • 1
    This concern is moot for the app instance itself, which is what is being stored in mContext, in Cristian's answer. The app instance has the same lifetime as your app; there is nothing to "leak". **The concern does become important if access to UI is needed**; in that case an `Activity` would be passed in as context, and it is important to have the activity make another call to "unregister" itself, in onPause and onDestroy. See http://stackoverflow.com/a/13994622/199364 for such a technique, when Activity context is needed. `static WeakReference mContext` might also be helpful. – ToolmakerSteve Oct 08 '15 at 02:41