Search code examples
android-annotationsgreenrobot-eventbus

AndroidAnnotations - Create Singleton EBean without adding the dependency


I am using androidannotations in an Android project and I have an EBean defined as:

@EBean(scope = Singleton)
class MyBean {
    public void someFunction() {
    }
}

This bean is created whenever it is first required, that is, whenever I have:

@Bean
MyBean myBean;

in some class. I am also using EventBus in this project, and a function in MyBean is a @Subscriber. This function is not called (when an Event is posted) because an instance of MyBean does not exist (since I don't have the dependency anywhere). How can I create an instance of MyBean without specifically adding the dependency anywhere?

My temporary solution is to simply define the bean in my Application class.


Solution

  • Turns out, you can't create an EBean without injecting it somewhere. Refer this issue: https://github.com/excilys/androidannotations/issues/1692

    My solution is to have a class EBeanManager (which is also an EBean) which simply injects the beans I need created. EBeanManager is injected into my Application class. The reason I'm doing it in a separate class is to avoid clutter in my Application class.