How to use Otto event bus with Android Annotations <p>I am working on an app where I want to use <a href="https://github.com/excilys/androidannotations" rel="nofollow">Android Annotations</a> and <a href="http://square.github.io/otto/" rel="nofollow">Otto Bus Event from Square</a></p> <p>For the integration of these two libraries together I have followed this <a href="https://github.com/excilys/androidannotations/wiki/OttoIntegration" rel="nofollow">link here</a>. Also I have used the <code>Otto 2.0-wip</code> lib that is suggested there and not that from <code>Otto Git</code>.</p> <p>This is how I do the implementation:</p> <p>I created a singleton class for the bus: </p> <pre><code>@EBean(scope = EBean.Scope.Singleton) public class BusProviderAA extends BasicBus{ } </code></pre> <p>I declare an object for this class inside <code>my Fragment Class</code> where I want to <code>subscribe</code> at events :</p> <pre><code>@Bean BusProviderAA ottoBus; @AfterInject protected void initAfterInjectMFragment() .... // my stuff here ottoBus.register(this); } @Override public void onStop() { super.onStop(); ottoBus.unregister(this); } @Subscribe public void onChangeUserDetailsEvent(ChangeUserDetailsEvent mEvent){ Log.e(" s onChangeUserDetailsEvent", "ss onChangeUserDetailsEvent"); if(mEvent.msg.contains("Data_changed")){ //TODO } } </code></pre> <p>I post event to the bus from <code>my communicator class</code> when it gets <code>callback</code> from server communication. This is how I <code>post event</code> to my bus inside this class:</p> <pre><code>@Bean //To use this enhanced class in another enhanced class or in an enhanced Android component, use @Bean: BusProviderAA ottoBus; public void callbackResponse(....){ ....// my stuff here and after callback ottoBus.post(changeUserDetailsEvent(serverResponse.getMsg())); } @Produce public ChangeUserDetailsEvent changeUserDetailsEvent(String msg){ return new ChangeUserDetailsEvent(msg); } </code></pre> <p>And this is my <code>ChangeUserDetailsEvent</code> class:</p> <pre><code>public class ChangeUserDetailsEvent { public final String msg; public ChangeUserDetailsEvent(String msg) { this.msg = msg; } } </code></pre> <p><strong>Problem:</strong> My problem is that my method which is subscribed at bus event <code>onChangeUserDetailsEvent</code> doesnt get called and I dont even know how to debug to fix this problem. </p> <p>I should mention that bus event works perfectly when I implement it in a <code>Fragment</code> that doesn't use Annotations, and a <code>Bus</code> singleton without Annotation. This is the Bus singleton class when I don't use Annotations:</p> <pre><code>public class BusProvider { private static final Bus BUS = new BasicBus(); // me lib public static Bus getInstance(){ return BUS; } private BusProvider(){} } </code></pre> <p>Thanks!</p> <p><strong>EDIT</strong></p> <p>I also have a problem when I update to <code>androidannotations:3.2</code> changing my <code>gradle file</code> from:</p> <p><code>compile 'org.androidannotations:androidannotations:3.1' apt 'org.androidannotations:androidannotations:3.1'</code> </p> <p>to: </p> <p><code>compile 'org.androidannotations:androidannotations:3.2' apt 'org.androidannotations:androidannotations:3.2'</code>. </p> <p>For version 3.1 it compiles but doesnt work <code>otto event bus</code>. With version 3.2 it gives error.This means that AA library has made some changes that I need to implement or it has bugs. How can I find the solution ? </p> <p>This is one of the errors(for demonstration): Error:(27, 25) error: cannot find symbol class UserAccountActivity_ which is a class that is activity.</p> <p>This is my androidannotations.log:</p> <pre><code>17:26:05.187 [Daemon Thread 13] INFO o.a.AndroidAnnotationProcessor:84 - Initialize AndroidAnnotations 3.2 with options {androidManifestFile=C:\Users\Armando\Android Studio\Hu\app\build\intermediates\manifests\full\debug\AndroidManifest.xml, resourcePackageName=XXPack} 17:26:05.244 [Daemon Thread 13] INFO o.a.AndroidAnnotationProcessor:108 - Start processing for 15 annotations on 100 elements 17:26:05.273 [Daemon Thread 13] DEBUG o.a.h.AndroidManifestFinder:98 - AndroidManifest.xml file found with specified path: C:\Users\Armando\Android Studio\Hu\app\build\intermediates\manifests\full\debug\AndroidManifest.xml 17:26:05.279 [Daemon Thread 13] INFO o.a.AndroidAnnotationProcessor:171 - AndroidManifest.xml found: AndroidManifest [applicationPackage=XXPack, componentQualifiedNames=[XXPack.SplashScreen, XXPack.HomeActivity, XXPack.ActivityProve, XXPack.UserAccountActivity_, XXPack.CartActivity_, com.facebook.LoginActivity], permissionQualifiedNames=[android.permission.INTERNET, android.permission.READ_EXTERNAL_STORAGE, android.permission.write_external_storage], applicationClassName=null, libraryProject=false, debugabble=false, minSdkVersion=14, maxSdkVersion=-1, targetSdkVersion=21] 17:26:05.280 [Daemon Thread 13] INFO o.a.r.ProjectRClassFinder:50 - Found project R class: XXPack.R 17:26:05.286 [Daemon Thread 13] INFO o.a.r.AndroidRClassFinder:44 - Found Android class: android.R 17:26:05.304 [Daemon Thread 13] INFO o.a.p.ModelValidator:42 - Validating elements 17:26:05.304 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with EActivityHandler: [XXPack.CartActivity, XXPack.UserAccountActivity] 17:26:05.306 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with EFragmentHandler: [XXPack.fragments.AddAddressFragment, XXPack.fragments.AddressBookFragment, XXPack.fragments.ChangePasswordFragment, XXPack.fragments.MyOrdersFragment, XXPack.fragments.MyVouchersFragment, XXPack.fragments.NewsLetterFragment, XXPack.fragments.PaymentMethodFragment, XXPack.fragments.PersonalDataFragment, XXPack.fragments.UserAccountFragment] 17:26:05.308 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with EBeanHandler: [XXPack.bus.BusProviderAA, XXPack.communicator.UserAccountCommunicator] 17:26:05.308 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with EViewGroupHandler: [XXPack.layouts.AddressBookItem, XXPack.layouts.CartItem, XXPack.layouts.OrdersItem, XXPack.layouts.UserAccountListFooter, XXPack.layouts.UserAccountListHeader] 17:26:05.319 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with ItemClickHandler: [lv_user_account(int)] 17:26:05.321 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with OptionsMenuHandler: [XXPack.fragments.UserAccountFragment] 17:26:05.323 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with BeanHandler: [ottoBus, ottoBus, communicator] 17:26:05.324 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with ProduceHandler: [produceNonceEvent(XXPack.models.Nonce), produceErrorEvent(XXPack.models.ErrorCommunication), produceFeatureCategoryEvent(java.util.ArrayList&lt;XXPack.models.SimpleCategory&gt;), produceErrorEvent(XXPack.models.ErrorCommunication), produceProductEvent(java.util.ArrayList&lt;XXPack.models.Product&gt;), produceErrorEvent(XXPack.models.ErrorCommunication), changeUserDetailsEvent(java.lang.String), produceErrorEvent(XXPack.models.ErrorCommunication), produceUserEvent(XXPack.models.User), produceErrorEvent(XXPack.models.ErrorCommunication)] 17:26:05.325 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by 17:26:05.325 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by 17:26:05.325 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by 17:26:05.325 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by 17:26:05.326 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by 17:26:05.326 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by 17:26:05.326 [Daemon Thread 13] ERROR o.a.h.AnnotationHelper:126 - @com.squareup.otto.Produce can only be used on a method with zero parameter, instead of 1 17:26:05.327 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by 17:26:05.327 [Daemon Thread 13] ERROR o.a.h.AnnotationHelper:126 - @com.squareup.otto.Produce can only be used on a method with zero parameter, instead of 1 17:26:05.327 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by 17:26:05.327 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by 17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by 17:26:05.328 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with SubscribeHandler: [onNonceEvent(XXPack.event.NonceEvent), onErrorEvent(XXPack.event.ErrorEvent), onFeaturesAndCategory(XXPack.event.FeatureCategoryEvent), onNonceEvent(XXPack.event.NonceEvent), onErrorEvent(XXPack.event.ErrorEvent), onUserEvent(XXPack.event.UserEvent), onChangeUserDetailsEvent(XXPack.event.ChangeUserDetailsEvent)] 17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by 17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by 17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by 17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by 17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by 17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by 17:26:05.328 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with AfterInjectHandler: [initAfterInjectCart(), initAfterInjectAddAddress(), initAfterInjectAddressBook(), initAfterInjectChangePass(), initAfterInjectAddressBook(), initAfterInjectPersonalData()] 17:26:05.328 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with AfterViewsHandler: [initAfterViewsCart(), initUserAccountAct(), initAddAddressFragment(), initAddressFragment(), initChangePassFragment(), initMyOrdersFragment(), initMyVoucherFragment(), initNewsLetterFragment(), initPaymentFragment(), initViewsAfterViews(), initUserAccountFragment()] 17:26:05.333 [Daemon Thread 13] INFO o.a.p.ModelProcessor:69 - Processing root elements 17:26:05.338 [Daemon Thread 13] DEBUG o.a.p.ModelProcessor:160 - Processing root elements EActivityHandler: [XXPack.UserAccountActivity, XXPack.CartActivity] 17:26:05.353 [Daemon Thread 13] DEBUG o.a.p.ModelProcessor:160 - Processing root elements EFragmentHandler: [XXPack.fragments.AddressBookFragment, XXPack.fragments.AddAddressFragment, XXPack.fragments.NewsLetterFragment, XXPack.fragments.MyOrdersFragment, XXPack.fragments.UserAccountFragment, XXPack.fragments.PaymentMethodFragment, XXPack.fragments.PersonalDataFragment, XXPack.fragments.ChangePasswordFragment, XXPack.fragments.MyVouchersFragment] 17:26:05.358 [Daemon Thread 13] DEBUG o.a.p.ModelProcessor:160 - Processing root elements EBeanHandler: [XXPack.bus.BusProviderAA, XXPack.communicator.UserAccountCommunicator] 17:26:05.358 [Daemon Thread 13] DEBUG o.a.p.ModelProcessor:160 - Processing root elements EViewGroupHandler: [XXPack.layouts.CartItem, XXPack.layouts.UserAccountListFooter, XXPack.layouts.OrdersItem, XXPack.layouts.AddressBookItem, XXPack.layouts.UserAccountListHeader] 17:26:05.363 [Daemon Thread 13] INFO o.a.p.ModelProcessor:77 - Processing enclosed elements 17:26:05.368 [Daemon Thread 13] INFO o.a.AndroidAnnotationProcessor:250 - Number of files generated by AndroidAnnotations: 18 17:26:05.368 [Daemon Thread 13] INFO o.a.g.ApiCodeGenerator:52 - Writting following API classes in project: [] 17:26:05.373 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.bus.BusProviderAA_ 17:26:05.388 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.layouts.AddressBookItem_ 17:26:05.456 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.layouts.CartItem_ 17:26:05.468 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.layouts.OrdersItem_ 17:26:05.480 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.layouts.UserAccountListFooter_ 17:26:05.491 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.layouts.UserAccountListHeader_ 17:26:05.500 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.CartActivity_ 17:26:05.514 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.UserAccountActivity_ 17:26:05.529 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.AddAddressFragment_ 17:26:05.546 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.AddressBookFragment_ 17:26:05.559 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.ChangePasswordFragment_ 17:26:05.587 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.MyOrdersFragment_ 17:26:05.600 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.MyVouchersFragment_ 17:26:05.613 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.NewsLetterFragment_ 17:26:05.624 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.PaymentMethodFragment_ 17:26:05.634 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.PersonalDataFragment_ 17:26:05.649 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.UserAccountFragment_ 17:26:05.664 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.communicator.UserAccountCommunicator_ 17:26:05.670 [Daemon Thread 13] INFO o.a.p.TimeStats:81 - Time measurements: [Whole Processing = 426 ms], [Generate Sources = 302 ms], [Process Annotations = 40 ms], [Extract Annotations = 27 ms], [Validate Annotations = 25 ms], [Find R Classes = 18 ms], [Extract Manifest = 6 ms], 17:26:05.671 [Daemon Thread 13] INFO o.a.AndroidAnnotationProcessor:122 - Finish processing </code></pre>