gwtgwt-history

What is need History.fireCurrentHistoryState() in GWT History?


Hello I am working on a GWT sample history management application. Here is my onModuleLoad Code.

public void onModuleLoad() {
    ContentPanel panel = ContentPanel.getInstance();
    if(History.getToken()!=null && History.getToken().length()==0)
    {
        History.newItem("first_page");
    }
    History.addValueChangeHandler(new HistoryHandler());
    RootPanel.get().add(panel);
    History.fireCurrentHistoryState();
}

In this I fired History.fireCurrentHistoryState(); to fire current state of history. Now In my firstPanel class ther is button named Second Panel on which history token second_page is fired.

public FirstPanel() {
    VerticalPanel panel = new VerticalPanel();
    Button button2 = new Button("Second Panel");
    button2.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            History.newItem("second_page");
        }
    });
    panel.add(button2);
    initWidget(panel);
}

But here no need to fire History.fireCurrentHistoryState() ; again. simply histtory.newItem works fine.

Now I want to know that what the need of History.fireCurrentHistoryState() at module load time only? Also why it is not required second time in the application.?


Solution

  • History.fireCurrentHistoryState() invokes your history handlers without actually inserting new history item in browser history stack, while History.newItem(token) does insert new history token into history stack.

    Note: if your current token is the same as new token (i.e. same page is reloaded), then browsers do not insert this into history stack. In this case (current token == new token) History.fireCurrentHistoryState() has the same effect as History.newItem(currentToken).