-
Notifications
You must be signed in to change notification settings - Fork 25
Events
Events are generated using the @Event annotation, you can generated automatically declaring a field with the class name:
@Event SomethingHappened event;
Or you can create it automatically in the method that will catch the event. See Catching events:
@Event
void onSomethingHappened() {
}
This will create the SomethingHappened event class.
For adding fields to the generated event, a string should be supplied to the annotation, indicating the field name and field class, ex: fieldName: String
@Event("fieldName: String") SomethingHappened event;
In the case of declaring the event in the catching method, you can simply pass the field name and field class as parameters:
@Event
void onSomethingHappened(String fieldName) {
}
Manually events can be also created:
@UseEvents
public class SomethingHappened {
String fieldName;
}
For each of the fields of an event class, a setter will be generated. This setter will return the instance of the event itself.
Events can be dispatched using the post method in the generated class:
SomethingHappened_.post();
To initialize the events fields, you should use the extended syntax to dispatch the event:
SomethingHappened_.create("fieldName value").postEvent();
//OR
SomethingHappened_.create().setFieldName("fieldName value").postEvent();
To catch an event in any Enhanced Component, the component should be annotated with @UseEventBus. The event can be catched in an method named "on" + "event class name". Optionally you can pass as parameters to this method, the fields of the event, or the event class itself
@UseEventBus
@EActivity(R.layout.activity_answers)
public class MyActivity extends Activity {
@Event
void onSomethingHappened(String fieldName) {
}
}
Example:
- [Events and Models Example](Events and Models Example) (Read first [Events and Actions in Model Injection](Events and Actions in Model Injection))
See also:
Sponsored by DSpot Sp. z o.o. Contact us at info@dspot.com.pl