3

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

kapandron
  • 3,546
  • 2
  • 25
  • 38
Sanjay Jain
  • 3,518
  • 8
  • 59
  • 93

1 Answers1

3

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).

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • thanks for reply.But I actually want to know why it required to dire at first time only. if "while History.newItem(token) does insert new history token into history stack." how it fired as we are not invoking History.fireCurrentHistoryState(). Hope I am clear with my views. – Sanjay Jain Aug 03 '11 at 12:48
  • 3
    You have to fire it because you install history handlers AFTER the page was already loaded, and you do not receive the history change event the first time. – Peter Knego Aug 03 '11 at 12:53