Browser closing events

Hi All,

I am facing one issue like when i am trying to close the browser then i want that i will get one message like “browser has been closed” ,for that i have used below code but its not working :

mainWindow = new Window();
System.out.println(“before Closing the application”);
mainWindow.addListener(new Window.CloseListener(){
@Override
public void windowClose(CloseEvent e) {
System.out.println(“browser has been closed”);
getMainWindow().getApplication().close();
}
});
System.out.println(“after Closing the application”);
setMainWindow(mainWindow);

When my application has started then i got below logs without clicking on close option on browser :

before Closing the application
after Closing the application

After clicking not getting any logs like i want logs should come “browser has been closed”.

Thanks in advance

–Wasim

There are many forum threads about this with more details, but the short answer is that this cannot be done reliably - some browsers simply don’t support this and the browser can crash or you can have network problems.

Using heartbeat and session expiration and doing cleanup latest on session expiration (if no event before that) can help, but often with a considerable delay.

Thanks for reply Henri…

but the same code i have tried in one small POC then it is working fine.please find below the POC code :

package com.example.closewindow;

import com.vaadin.Application;
import com.vaadin.ui.*;
import com.vaadin.ui.Window.CloseEvent;

/**

  • Main application class.
    */
    public class ClosewindowApplication extends Application {

    @SuppressWarnings(“serial”)
    @Override
    public void init() {
    Window main = new Window(“Closewindow Application”);

     main.addListener(new Window.CloseListener(){
    
     	public void windowClose(CloseEvent e) {
     		// TODO Auto-generated method stub
     		System.out.println("Closing the application");
           getMainWindow().getApplication().close();
     	} 
     	});
     
     setMainWindow(main);
     
     
     Label label = new Label("Hello World");
     main.addComponent(label);
    

    }

}

I have one Vaadin Application where i am trying to integrate the same below piece of code but I don’t know why it is not working and what is the reason behind it.
The vaadin jar(vaadin-6.8.12.jar) on both the application are same access specifier are all same.The page over which i was applying is the base page extending Application.

Please suggest me if i can have another better option on browser close button listener.

Thanks in advance.

–Wasim