An Android Annotations plugin to support the Greenrobot Eventbus.
It can inject the default EventBus and automatically handle the registration/unregistration.
In the build.gradle file of the module project:
dependencies {
// Android Annotations for Greenrobot
apt 'com.tmtron:green-annotations:1.0.2'
compile 'com.tmtron:green-annotations-api:1.0.2'
}Note: Green-Annotations version 1.0.2 requires Android-Annotations version 4.4.0
Shows the Activity without using AndroidAnnotations or GreenAnnotations:
publicclassActivityBeforeextendsActivity {
EventBuseventBus;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
eventBus = EventBus.getDefault();
}
@OverridepublicvoidonStart() {
super.onStart();
eventBus.register(this);
}
@OverridepublicvoidonStop() {
eventBus.unregister(this);
super.onStop();
}
publicvoidfireEvent(Stringmessage) {
eventBus.post(newMessageEvent(message));
}
@Subscribe(threadMode = ThreadMode.MAIN)
publicvoidonMessageEvent(MessageEventevent) {
Toast.makeText(getApplicationContext(), event.message, Toast.LENGTH_SHORT).show();
}
}Shows the same Activity with AndroidAnnotations and GreenAnnotations:
@EActivitypublicclassActivityAfterextendsActivity {
@EventBusGreenRobotEventBuseventBus;
publicvoidfireEvent(Stringmessage) {
eventBus.post(newMessageEvent(message));
}
@Subscribe(threadMode = ThreadMode.MAIN)
publicvoidonMessageEvent(MessageEventevent) {
Toast.makeText(getApplicationContext(), event.message, Toast.LENGTH_SHORT).show();
}
}Note: green-annotations-test is a tiny test-project: a sample android app with only one single activity to show how to use green-annotations (and the gradle-setup).
This plugin is under the Apache 2.0 license. Copyright 2016, Martin Trummer