Browser history management is an important aspect of a web application. An optimized history management will allow faster navigation and better responsiveness. The reason why GWT constitues a special case is its use of a single page (called RootPanel) approach ( SPA). The page can be seen as board on which components are added or deleted depending on the current context. Without history management in GWT, the application user will face the following issues:
          –  If the user hits the back button, the browser will navigate to the previous page used by the user before entering the application and not to the previous state of the application.
           – If the user wants to bookmark the current state of the application, the bookmark will point to the first page of the application.

To tackle these issues, GWT introduced history management mechanisms starting from its 2.1 release. Activity and Place are helper classes that allow developpers to manage history by assigning names to application states using # ( Fragment Identifier). They work best in concordance with an MVP architecture. In this tutorial, we will go throught how to use Activities and Places to effectively manage history in a GWT application.

GWT plugin
the application we built last tutorial: 

Our application has two pages or states: a login and main page.

First of all, we are going to change our MVP setup. We are going to move our Presenter interface inside our view, and have it implemented by the activity which will play the role of the Presenter as well.

public class LoginView extends Composite implements IsWidget {
 HorizontalPanel container;
 Label loginLabel;
 Label passwordLabel;
 TextBox loginField;
 PasswordTextBox passwordField;
 Button loginButton;
 private Presenter presenter;

 public HasClickHandlers getLoginButton() {
  return loginButton;
 public LoginView(){
  container = new HorizontalPanel();
  loginField = new TextBox();
  loginButton = new Button("Login");
  passwordField = new PasswordTextBox();
  loginLabel = new Label("Login");
  passwordLabel = new Label("Password");

 public Widget asWidget() {
  return container;
 public Presenter getPresenter() {
  return presenter;

 public void setPresenter(Presenter presenter) {
  this.presenter = presenter;

 public interface Presenter{
        public void goTo(Place place);
        public void loginButtonEvent();


Next we are going to create the LoginPlace and LoginActivity, which are required for managing history. A Place object holds the application state at a particular point.

public class LoginPlace extends Place {
 String name;
 public LoginPlace(String placeName){ = placeName;
 public String getPlaceName(){ 
  return name;
  public static class Tokenizer implements PlaceTokenizer<LoginPlace> {
         public String getToken(LoginPlace place) {
             return place.getPlaceName();
         public LoginPlace getPlace(String token) {
             return new LoginPlace(token);

As you may notice, the LoginPlace class has an inner static class called Tokenizer which implements the PlaceTokenizer. This last is used for serializing the place name into a token that will be used by the browser to refer to the place. The next step is to define Before doing so, we need to define our client factory which will be used for dependency injection.

public interface ClientFactory {
     LoginView getLoginView();
     MainPageView getMainPageView();
     AppController getAppController();
     EventBus getEventBus();
     PlaceController getPlaceController();

public class ClientFactoryImpl implements ClientFactory {
 LoginView login = new LoginView();
 MainPageView mainPage = new MainPageView();
 HandlerManager controllerBus = new HandlerManager(null);
 EventBus eventBus = new SimpleEventBus();
 PlaceController controller = new PlaceController(eventBus);

 public LoginView getLoginView() {
  return login;

 public MainPageView getMainPageView() {
  return mainPage;

 public EventBus getEventBus() {
  return eventBus;

 public PlaceController getPlaceController() {
  return controller;


We need add this piece of code in our module definition xml file.

<replace-with class="com.opencode.client.ClientFactoryImpl">
    <when-type-is class="com.opencode.client.ClientFactory">

Now, we are ready to define our activity :

public class LoginActivity extends AbstractActivity implements LoginView.Presenter {
 ClientFactory factory;
 String name;
 public LoginActivity(LoginPlace loginPlace, ClientFactory clientFactory){
  this.factory = clientFactory; = loginPlace.getPlaceName();

 public void start(AcceptsOneWidget panel, EventBus eventBus) {
  LoginView view = factory.getLoginView();
 public void bindEvents(){

 public void loginButtonEvent() {
  factory.getLoginView().getLoginButton().addClickHandler(new ClickHandler(){
   public void onClick(ClickEvent event) {
    System.out.println("inside event");
    goTo(new MainPagePlace("MainPage"));
 public void goTo(Place place) {

The next step is to define an ActivityMapper which maps each Place to an Activity. In addition, we are going to define a HistoryMapper that handles all the place tokens of our application.

public class MyActivityMapper implements ActivityMapper {
 private ClientFactory clientFactory;  
 public MyActivityMapper(ClientFactory factory){
  this.clientFactory = factory;
 public Activity getActivity(Place place) {
  if(place instanceof LoginPlace){
   return new LoginActivity((LoginPlace) place, clientFactory);
  }else if(place instanceof MainPagePlace){
   return new MainPageActivity((MainPagePlace)place, clientFactory);
  }else if(place instanceof UserSelectPlace){
   return new MainPageActivity((MainPagePlace)place, clientFactory);
  return null;

@WithTokenizers({LoginPlace.Tokenizer.class, MainPagePlace.Tokenizer.class})
public interface MyHistoryMapper extends PlaceHistoryMapper  {

Finally, we need to update our entry point with all the new components:

public class MVPexample implements EntryPoint {
 private LoginPlace welcomePlace = new LoginPlace("login");
 private SimplePanel appWidget = new SimplePanel();
 public void onModuleLoad() {
  ClientFactory clientFactory = GWT.create(ClientFactory.class);
  PlaceController controller = clientFactory.getPlaceController();
  EventBus bus = clientFactory.getEventBus();
        ActivityMapper activityMapper = new MyActivityMapper(clientFactory);
        ActivityManager activityManager = new ActivityManager(activityMapper, bus);

        MyHistoryMapper historyMapper= GWT.create(MyHistoryMapper.class);
        final PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
        historyHandler.register(controller, bus, welcomePlace);


Same thing goes for

Full example code available at: