This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
OttoIntegration
Kay-Uwe Janssen edited this page Oct 9, 2016
·
21 revisions
Since AndroidAnnotations 3.0
- Add AndroidAnnotations to your project.
- Add the
otto
AA plugin to your project -
Add
otto
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
and @Produce
annotations can be used in any enhanced component.
19/11/2020 The 4.8.0 release is out !
- Get started!
- 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
- Ask on Gitter