I have to resolve resources (server) if a user leaves a page (Browser tab).
If the user moves around the page, DetachNotifier.addDetachListener works great. If he closes the whole browser, I can use VaadinService.addSessionDestroyListener.
But how can I catch if a user closes the browser tab, even without closing other tabs of the same application?
Probably not reliable, but better than nothing for watching closed / refreshed browser tabs:
The Route defines a layout class, e.g. @Route(value = "", layout = MyLayout.class).
This layout class can be extended with implements DetachNotifier and, in the constructor, a
addDetachListener(e -> removeAll()); // Called if tab is closed / reloaded and heartbeat stopped. Not fully reliable
removeAll() triggers the detachListener of all contained Components and makes sure it won’t be called twice.
If this doesn’t work, SessionDestroyListener gotta do the job.