javagwtgwt-mvpgwt-placesgwt-activities

Understanding GWT AbstractActivity's start method


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:

  1. What is containerWidget? Where does it come from? Is it what is attached to the RootPanel? In other words, what is the value of setting our View to it?
  2. Why does the AbstractActivity#start(...) method accept an EventBus arg? Is it required and/or typical to send/receive events to/from the bus from inside this method?

Solution

  • 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 ActivityManagers in response of URL changes (i.e., PlaceChangeEvents).

    An ActivityManager is in charge of a given area of the UI, a display (of course, if you have multiple managers; the whole UI - the body - in the other case). Such managers internally hold a reference to the AcceptsOneWidget they own (the one you pass from a call to ActivityManager.setDisplay() method), as well as an EventBus object (the one you pass from an ActivityManager instantiation).

    So...

    1. containerWidget is the display. It could be a RootLayoutPanel.get(), or a specific panel of the whole UI (must be an AcceptsOneWidget one). You attach your view, to it.
    2. The EventBus of the start() method is a ResettableEventBus wrapper of the original one holded by the ActivityManager. 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.