I'm writing an Android app that has some functionality encapsulated in an internal library. However, for this functionality to work, the library needs an instance of the Application Context. What's the best way to give the library this Context? I see a few options, none of them appealing:
Application
, and call getApplicationContext()
Context
each time they get a reference to the singleton.
What's the best way to give the library this Context?
Pass a Context
into the methods exposed by your library that need a Context
. This is what the Android SDK does in places.
Or, change your library to expose objects, not static methods, and have the objects hold an instance of Context
(supplied to the constructor or factory method that created the instance).
Have my library classes extend Application, and call getApplicationContext()
If you can call getApplicationContext()
on a Context
, you would just do that, without needing to subclass Application
.