views:

387

answers:

3

For example current page is www.google.com. But I typed a different website address in address bar and clicked. This site has fully GWT code.

But I like to back to the previous page of www.google.com. So I clicked back button of browser.but how can I get event of back button from current GWT code. Can I set any backbutton event handler in GWT of current page? One which notifies an alert to me that back button was pressed.

Is there any solution from GWT?

+1  A: 

You can implement the HistoryListener interface: your class's method onHistoryChanged will be called (with a String token) on every click to the back and forward buttons. You can then interact with the History class, which has e.g. a back() static method to "go back". However, I'm not entirely sure if it goes back all the way to before GWT started (but it's sure worth trying;-).

Alex Martelli
`HistoryListener` and in general *Listeners are deprecated as of GWT 1.6 - you should use the appropriate Handlers instead (in this case it's a `ValueChangeHandler`, which might not be as obvious as say `HistoryHandler`).
Igor Klimer
+3  A: 

There's Window.ClosingEvent:

Fired just before the browser window closes or navigates to a different site.

The other option is History.addValueChangeHandler, which listens for changes in the browser's history stack (see the docs for more info).

Igor Klimer
A: 

+1 to Igor and Alex. Here's some code you can use, if you want to use the ClosingHandler:

    Window.addWindowClosingHandler(new Window.ClosingHandler() {

        @Override
        public void onWindowClosing(final ClosingEvent event) {
            event.setMessage("Don't you think my site is awesome?");
        }
    });

Some info from the Javadoc of ClosingHandler.onWindowClosing():

 /* Fired just before the browser window closes or navigates to a different
  * site. No user-interface may be displayed during shutdown. */
Chris Lercher