当用户试图离开我的 GWT 应用程序时,我想显示一个确认对话框。如果用户选择留在应用程序中,则不执行任何操作,否则应用程序应在关闭之前先向服务器发送注销请求。
我怎样才能做到这一点?
使用哪个处理程序?
Window.addCloseHandler
或者
Window.addWindowClosingHandler
Based on the methods it has, addWindowClosingHandler
seems to be what you need.
EDIT: I think what you need to do is... addWindowClosingHandler
is called when the window is closing, i.e. when the user clicks the close or reload button. addCloseHandler
is called when the Window closes. So you use both! You use the closing handler to display a confirmation dialog, then you use the close handler to do the stuff you only want to do on close.
Window.addWindowClosingHandler(new Window.ClosingHandler() {
@Override
public void onWindowClosing(ClosingEvent event) {
event.setMessage("Do you wanna close?");
System.out.println("Closing...");
}
});
Window.addCloseHandler(new CloseHandler<Window>() {
@Override
public void onClose(CloseEvent<Window> event) {
System.out.println("Closed!");
}
});