Closed zenghou closed 6 years ago
Hey, This was actually covered in the original ADB4 dev guide:
So Google's EventBus is used to handle this in ADB4. The EventsCenter class provides a few simple methods for you to use. The thing to note about this event-driven design, is that neither the firer or the handler require any knowledge of the other party.
Here's an example
To fire the event
To handle the event:
You need to first register the class as an event handler, by using the following method (you can call this in a constructor):
EventsCenter.getInstance().registerHandler(this);
Simply add the annotation @Subscribe (be sure to make the necessary import of, com.google.common.eventbus.Subscribe
) on top of the handling method in the class that you wish for the event to be handled. Include the Event class to be handled as a parameter for this method(it should be the only parameter, since you should never have to call this method explicitly):
Notice how the IDE is greying it out, as there is no explicit call to this method, since the eventbus handles this for us
You should extend the BaseEvent
class provided if you want to create your own events. Any new events you create can be used to store information you want to pass to the handler, from the firer
The method is executed whenever it detects this event being fired from anywhere.
@philemontan Hey Philemon, thanks for this clarification! It helped me get to what I was looking for. I overlooked the dev guide but I'll be sure to check it next time.
Kudos for the excellent answer @philemontan 💯
Hello all,
I am currently working on user authentication. The rough outline is to have create a
UserCreds
object withinModelManager
. When the login command is successfully executed, a boolean attributeisValidated
that is withinUserCreds
is changed to true. Upon this change, I want to getUIManager
to execute a method.I looked at
ModelManager
and had an idea about raising an event. However, I'm slightly confused as to how theUiManager
is informed of this event. Can someone kindly enlighten me? If i'm on the wrong path, can someone kindly tell me how else I can get the UI to respond to a change in the Model?Any help here would be greatly appreciated! Thanks!