I'm trying to learn both GWT-recommended MVP and their Activities & Places API (yes, I know they are two different things, but they seem play nicely into each other).
In a lot of code examples of Activities/Places, I keep seeing the following similar code in the AbstractActivity
impls:
@Override
public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
view.setPresenter(this);
containerWidget.setWidget(view.asWidget());
}
I believe the first line (view.setPresenter(this);
) is to create bi-directionality between the View and the Presenter. But I'm not sure what the 2nd line (containerWidget.setWidget(view.asWidget());
) accomplishes. So, 2 questions:
- What is
containerWidget
? Where does it come from? Is it what is attached to theRootPanel
? In other words, what is the value of setting our View to it? - Why does the
AbstractActivity#start(...)
method accept anEventBus
arg? Is it required and/or typical to send/receive events to/from the bus from inside this method?
Activities are objects in charge of the interactions on a given part of the UI, in a given time. They are started/stopped by their relative
ActivityManager
s in response of URL changes (i.e.,PlaceChangeEvent
s).An
ActivityManager
is in charge of a given area of the UI, a display (of course, if you have multiple managers; the whole UI - thebody
- in the other case). Such managers internally hold a reference to theAcceptsOneWidget
they own (the one you pass from a call toActivityManager.setDisplay()
method), as well as anEventBus
object (the one you pass from anActivityManager
instantiation).So...
containerWidget
is the display. It could be aRootLayoutPanel.get()
, or a specific panel of the whole UI (must be anAcceptsOneWidget
one). You attach your view, to it.EventBus
of thestart()
method is aResettableEventBus
wrapper of the original one holded by theActivityManager
. This way, when the activity is stopped, any handler attached to such bus, will be automatically de-registered. You generally want to rely on this bus, rather than using the global one.