Skip to content
PerfectCarl edited this page Dec 29, 2013 · 18 revisions

Since AndroidAnnotations 3.0

AndroidAnnotations integrates with Otto 2.0-wip. This version has yet to be released and work happens 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.

Getting Otto 2.0-wip

You can download a [build here] (http://eeee) 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>.

Integrating Otto and AndroidAnnotation

  1. Add AndroidAnnotations to your project.
  2. Add otto-2.0.0-SNAPSHOT to your project.
  3. Use @Subscribe and @Post annotations to declare the event you want to broker between your components.
  4. Looking for a working example? Have a look CleanAndroidCode!

The following code (taken from the CleanAndroidCode) shows you how an Activity notifies its Fragment that the title has been updated.

public class UpdateTitleEvent {

        public final CharSequence title;

        public UpdateTitleEvent(String title) {
                this.title = title;
        }

}
@EActivity(R.layout.hello_activity)
public class HelloAndroidActivity extends BaseActivity {

	@Subscribe
	public void onUpdateTitle(UpdateTitleEvent event) {
		setTitle(event.title);
	}

}
@EFragment(R.layout.hello_fragment)
public class HelloFragment extends BaseFragment {

	@Inject
	Bus bus;

	@Click
	void fragmentButtonClicked() {
		bus.post(new UpdateTitleEvent("Button clicked"));
	}
}

The @Subscribe annotation

The @Post annotation

The @Produce annotation

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Clone this wiki locally