1

I'm trying to run some RPC calls when the user closes the window, refreshes it or clicks the back button but just for one single page. I found a post talking about handling but the solution is not working well, missing back button handler (not working) and always is for all page on the web, I can't find something for remove handler if you leave from page

    Window.addWindowClosingHandler(new Window.ClosingHandler() {

                @Override
                public void onWindowClosing(ClosingEvent event) {
                    event.setMessage("You sure?");
                }
            });

    Window.addCloseHandler(new CloseHandler<Window>() {

                @Override
                public void onClose(CloseEvent<Window> event) {

                    // Execute code when window closes!
                System.out.println("ble ! ");

                }
            });

Framework: GWT 2.4 with mvp4g.
Browsers: FF and Chrome.

gfelmer
  • 31
  • 1
  • 6
  • "Not work" is not a description of a problem. What's not working? Any errors? Code not being called? http://stackoverflow.com/faq#howtoask – Strelok Aug 03 '12 at 01:53
  • Pressing back button is not showing the message dialog, thats not working, unlike close browser/tab, change url or refresh that fire the dialog. – gfelmer Aug 03 '12 at 13:57

2 Answers2

1

Because i use mvp4g framework i found a solution there , you need to extends your presenter with CyclePresenter and override onLoad and onUnload methods. These methods fire when view is load/unload from DOM, i tested and work for all cases, f5, back button, close browser/tab, go other web and call others events. Now i cant put some code there.

gfelmer
  • 31
  • 1
  • 6
0

You need to remove the handler when you leave the page and then re-add it when you enter the page again. You have the "add" side covered with the above code, you are missing the "remove" part. You do that by using the HandlerRegistration object that is returned from the add*Handler methods. When you want to remove the registered handler, you just call the HandlerRegistration.removeHandler() method:

HandlerRegistration windowClosingHandler = Window.addWindowClosingHandler(new ClosingHandler() {
    @Override
    public void onWindowClosing(ClosingEvent event) {
        // Handle window closing
    }
});
// From now on the CloseHandler will be fired

// ...
// Somewhere else:
windowClosingHandler.removeHandler();
// From now on the CloseHandler won't be fired
Igor Klimer
  • 15,321
  • 3
  • 47
  • 57
  • Igor, removeHandler its working but when user click back button the method onClose (CloseHandler) not execute and i cant remove handler. – gfelmer Aug 03 '12 at 16:06