I'm currently using Vaadin Flow version 14 (https://github.com/vaadin/platform/releases/tag/14.0.0)
I run Java version 1.8.0_231, 64 bit.
I simply want to be able to detect (in java!) whenever a user does either of these actions:
- Closes the current browser tab (or their browser)
- Clicks F5 on their keyboard, or pressed the Refresh button in their browser
- Clicks a link (or anything else), which redirects them away from my website
I've tried lots of different things to detect this. The only thing I have been able to detect so far, is whenever the current VaadinSession expires (which I can change by doing VaadinSession.getCurrent().getSession().setMaxInactiveInterval(15)
). This makes every session expire after 15 seconds (15 is just a testing number in my case).
With Vaadin 8, I believe you can just do this and it will work out of the box:
JavaScript.getCurrent().execute(
"function closeListener() { catchClose(); } " +
"window.addEventListener('beforeunload', closeListener); " +
"window.addEventListener('unload', closeListener);"
);
JavaScript.getCurrent().addFunction("catchClose", arguments ->
{
System.out.println("user has quit :o");
});
I can't use this tho, since I use Vaadin 14, not 8.
I have tried adding this to my View class:
@Override
protected void onDetach(DetachEvent detachEvent)
{
System.out.println("onDetach " + detachEvent);
}
It only prints this message when the session expires (15 seconds in my case). Even if I don't close the page.
I have tried implementing BeforeLeaveObserver / BeforeLeaveListener and overridding this:
@Override
public void beforeLeave(BeforeLeaveEvent beforeLeaveEvent)
{
System.out.println("beforeLeave");
}
This never prints.
I've also tried all of these, but they do not do what I need:
VaadinResponse.getCurrent().getService().addUIInitListener(e ->
{
System.out.println("1 addUIInitListener : " + e);
e.getUI().addBeforeLeaveListener(e2 ->
{
System.out.println("1.1 addBeforeLeaveListener : " + e2);
});
e.getUI().addDetachListener(e2 ->
{
System.out.println("1.2 addDetachListener : " + e2);
});
});
VaadinResponse.getCurrent().getService().addServiceDestroyListener(e ->
{
System.out.println("2 addServiceDestroyListener : " + e);
});
VaadinResponse.getCurrent().getService().addSessionDestroyListener(e ->
{
System.out.println("3 addSessionDestroyListener : " + e);
});
1 addUIInitListener
gets printed when the user loads the page.
1.1 addBeforeLeaveListener
never prints.
2 addServiceDestroyListener
never prints. A Service isn't the same as a Session. Makes sense tho.
1.2
and 3 addSessionDestroyListener
prints wafter awhile. Takes like 15-30 seconds tho.
Is there not a way to detect this basically instantly? :/