I have a library (aar) that needs a context application because it is using some API methods where a context is needed (WifiManager for example).
I saw there and in many other topics that the common way to achieve this is to pass the Context as a parameter to the object of my lib that needs it. My problem came when reading this article. The lib is to be used by external clients, so I want to avoid all null pointer exception at all cost (you know the rule: if it CAN happen, it WILL happen at some point).
Let's say I have a main entry point in my library, and this class is a Singleton and saves the context (in ctor or via a setter). I would like this class to "host" the Context and behave a little like the Application class: it will provide the context to the other classes of my library.
How can I access this singleton class from the "children" classes or other classes following the principles described in nfrolov's article ?
Let's see some (pseudo) code, with the classes MainSingleton
, NetworkManager
(singleton too) and DataProvider
.
DataProvider
can be a member of MainSingleton
and doesn't need context, but NetworkManager
needs the context to use WIFI_SERVICE
and registerReceiver
method.
However, DataProvider
needs info from NetworkManager
, either via registering a listener to it, or getting the data from MainSingleton
(that which be used like an event bus to coordinate things between children classes, like we can do with Activities and multiple Fragments).
Case 1
public class MainSingleton
{
private static MainSingleton instance = new MainSingleton();
private static Context context = App.getAppContext(); // where the frog do I get that from ?
...
}
Problem : as this is the main entry point, I don't have access to "App" in here, so it should be passed as a parameter somewhere, as a setter. But then the context is initialized to null ?
Case 2
public class MainSingleton {
private static MainSingleton instance = null;
private Context context;
private MainSingleton(Context context) {
this.context = context;
}
public synchronized static MainSingleton getInstance(Context context) {
if (instance == null) {
instance = new MainSingleton(context.getApplicationContext());
}
return instance;
}
}
Problem: Every "child" instance that need to call the MainSingleton class will need a context too.
Edit
After reading this post: I will most probably consider that my MainEntryPoint is never unloaded when the client application embedding my aar is running, pass the Context as a parameter in an init method and keep that static.