Understanding GWT AbstractActivity's start method

367 views Asked by At

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?
3

There are 3 answers

0
Andrea Boscolo On BEST ANSWER

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.
0
Christian Kuetbach On

view.setPresenter(this) set the bidirectional communication between view and presenter. Its meat to be used for click handlers and such things.

  1. yes. The RootPanel is mot likely the body-element. Within your onModuleLoad you create your application layout. There you will create a "AcceptsOneWidget", a SimplePanel for example and pass it to your presenter. The first thing you will do is create you own view. This view will replace the former content of this element.

  2. The EventBus is for the communication between all active Activities. Lets say you have a two column application layout. In this case you will have an active master Activity and a second detail Activity

Take a look at the Activity and Places section of gwtproject.org

0
Ronan Quillevere On

I think previous answers are good enough. Following schemas may help you understand workflow more easily.

Have a look to Thomas Broyer article on nesting activities http://blog.ltgt.net/gwt-21-activities-nesting-yagni/

I created a github project to implement his idea (still work in progress) : https://github.com/ronanquillevere/GWT-Multi-Activities

activityplacediagram

activityplacediagram2

Those are taken from articles inside my blog for more detail if needed http://wpamm.blogspot.fr/