I have a 3rd party library that exposes its functionality through a listener interface. The requirement of the library is that it gets initialized in the onCreate of the custom Android Application class.
public class CustomApplicationWithListener extends Application implements ThirdPartyListener {
public void onCreate() {
if(feature.isTurnedOn()){
// the library requires an application instance of type ThirdPartyListener
init(this);
}
}
}
I was trying to hide this functionality behind a feature flag and was wondering if there is a way to abstract the listener from the application class and only define it when the feature is needed. Only then we declare that interface. I know the custom application class needs to be defined in the manifest. Is there a way to dynamically decide at runtime that if the feature is enabled then initialize the base custom application object with the third party listener something like below and let the main manifest know the version we are going to use?
var application: Application?
if(feature.isTurnedOn()){
application = CustomApplicationWithListener()
} else {
application = CustomApplication
}