-
Notifications
You must be signed in to change notification settings - Fork 0
OttoIntegration
Since AndroidAnnotations 3.0
AndroidAnnotations integrates with Otto 2.0-wip.
This version has yet to be released and is developed in a specific [2.0-wip branch] (https://github.com/square/otto/tree/2.0-wip). [This ticket] (https://github.com/square/otto/issues/61) tracks Otto's progress on that matter.
You can download a [build here] (https://dl.dropboxusercontent.com/u/17850028/otto-2.0.0-SNAPSHOT.jar) or build it yourself:
git clone https://github.com/square/otto -b2.0-wip
cd otto
mvn clean install -DskipTests
The output jar otto-2.0.0-SNAPSHOT.jar
is located in projects/libraries/otto
.
- Add AndroidAnnotations to your project.
- Add
otto-2.0.0-SNAPSHOT
to your project. - Create a
singleton
class for the bus that can be injected with AA using the@EBean
annotation. - Create the event class that will transit through the bus.
- Post a new event to the bus:
bus.post( ...)
- Use
@Subscribe
annotation to get the published events.
The following code (taken from the CleanAndroidCode) shows you how an Activity
notifies its Fragment
that the title has been updated.
// Declare the bus as an enhanced bean
@EBean(scope = Scope.Singleton)
public class OttoBus extends BasicBus {
}
public class UpdateTitleEvent {
public final String title;
public UpdateTitleEvent(String title) {
this.title = title;
}
}
@EActivity(R.layout.hello_activity)
public class HelloAndroidActivity extends FragmentActivity {
@Bean
OttoBus bus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bus.register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
bus.unregister(this);
}
@Subscribe
public void onUpdateTitle(UpdateTitleEvent event) {
setTitle(event.title);
}
}
@EFragment(R.layout.hello_fragment)
public class HelloFragment extends Fragment {
int counter = 1;
@Bean
OttoBus bus;
@Click
void fragmentButtonClicked() {
bus.post(new UpdateTitleEvent("Clicks: " + counter++));
}
}
The @Subscribe
annotation can be used from within the following classes:
- [Activity] (Enhance-activities)
- Fragment
- [Bean] (Enhance-custom-classes)
The @Produce
annotation can be used from within the following classes:
- [Bean] (Enhance-custom-classes)
AndroidAnnotations was created by Pierre-Yves Ricau and is sponsored by eBusinessInformations.
09/11/2014 The 3.2 release is out !
- Get started!
- Download
- Cookbook, full of recipes
- Customize annotation processing
- List of all available annotations
- Release Notes
- Examples
- Read the FAQ
- Join the Mailing list
- Create an issue
- Tag on Stack Overflow