I am working on an app where I want to use Android Annotations and Otto Bus Event from Square
For the integration of these two libraries together I have followed this link here. Also I have used the Otto 2.0-wip
lib that is suggested there and not that from Otto Git
.
This is how I do the implementation:
I created a singleton class for the bus:
@EBean(scope = EBean.Scope.Singleton)
public class BusProviderAA extends BasicBus{
}
I declare an object for this class inside my Fragment Class
where I want to subscribe
at events :
@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
}
}
I post event to the bus from my communicator class
when it gets callback
from server communication.
This is how I post event
to my bus inside this class:
@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);
}
And this is my ChangeUserDetailsEvent
class:
public class ChangeUserDetailsEvent {
public final String msg;
public ChangeUserDetailsEvent(String msg) {
this.msg = msg;
}
}
Problem:
My problem is that my method which is subscribed at bus event onChangeUserDetailsEvent
doesnt get called and I dont even know how to debug to fix this problem.
I should mention that bus event works perfectly when I implement it in a Fragment
that doesn't use Annotations, and a Bus
singleton without Annotation. This is the Bus singleton class when I don't use Annotations:
public class BusProvider {
private static final Bus BUS = new BasicBus(); // me lib
public static Bus getInstance(){
return BUS;
}
private BusProvider(){}
}
Thanks!
EDIT
I also have a problem when I update to androidannotations:3.2
changing my gradle file
from:
compile 'org.androidannotations:androidannotations:3.1'
apt 'org.androidannotations:androidannotations:3.1'
to:
compile 'org.androidannotations:androidannotations:3.2'
apt 'org.androidannotations:androidannotations:3.2'
.
For version 3.1 it compiles but doesnt work otto event bus
. 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 ?
This is one of the errors(for demonstration): Error:(27, 25) error: cannot find symbol class UserAccountActivity_ which is a class that is activity.
This is my androidannotations.log:
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<XXPack.models.SimpleCategory>), produceErrorEvent(XXPack.models.ErrorCommunication), produceProductEvent(java.util.ArrayList<XXPack.models.Product>), 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
AndroidAnnotations 3.1 had a bug: it did not process the
Produce
andSubscribe
annotations. In AA 3.2 i fixed this problem.You can ask why AA has to process these annotations at all? Unfortunetaly Otto cannot read annotations from parent classes. But with AA, you always use the generated subclass. It means when Otto processes the generated subclass it does not find any Otto annotations, that is why your event handler method did not get called. To work around this problem, we added two annotation handlers, which process
Subscribe
andProduce
. These handlers only override the method in the generated class and copy the Otto annotations to those methods. This way Otto can read the annotations in the generated class.For example:
The
@Produce
annotated cannot have any arguments, since Otto cannot figure out what you want to pass to it when it calls the method (Otto doc).