How to Execute Code When the GWT Application Is Going Down

My goal was to store the UI state of my appli­ca­tion just before it gets ter­mi­nat­ed to be able to restore it next time the way the user left it the oth­er day.

I tried to add an addAttachHandler to the RootPanel to get informed about the root pan­el get­ting detached from the DOM so that I can final­ize my appli­ca­tion. Sur­pris­ing­ly, that does not work in Chrome (test­ed Chrome and Fire­fox only).

But besides that this sounds like a bug to me, I found the “prop­er” way of doing things before the appli­ca­tion ends: 

Window.addWindowClosingHandler(new Window.ClosingHandler() {
	@Override public void onWindowClosing(ClosingEvent event) { ... } 
});

In the end, I think some­thing like Document.addUnloadHandler would be more sug­ges­tive… clos­ing the win­dow or reload­ing a page is both exit­ing the appli­ca­tion by unload­ing the DOM — not clos­ing the win­dow.

Leave a Reply

Your email address will not be published. Required fields are marked *