Detect when the view/route has gone away

Hi All,

Is there a mechanism to detect when the current view has been closed off or navigated away from? Specifically, how do I know when the user had ‘navigated away’ so I know to stop the background threads associated with the component in the view.

I hope that makes sense… and thanks.

Stuart.

Hi,

sounds like you’re looking for either the BeforeLeaveEvent or the AfterNavigationEvent. They can be observed by implementing the BeforeLeaveObserver and the AfterNavigationObserver interfaces respectively; take a look at https://vaadin.com/docs/v10/flow/routing/tutorial-routing-lifecycle.html for some more details.

-Olli

Hi Olli,

I’d actually implemented onDetach() and now the beforeLeave() but they don’t get triggered when closing the tab/browser or reloading the page.

(It works great if I navigate away with-in the app, with either onDetach or beforeLeave :slight_smile: )

-Stuart

Yeah, there’s no way to get reliable events from a closing browser window or browser tab. I think you will need to handle any unclosed resources in a SessionDestroyListener (add with VaadinService.getCurrent().addSessionDestroyListener(e -> {}) )

-Olli

Hi Olli,

I’ve implemented a sessionDestroyListener, which seems to be working, I just need to determine a reasonable heartbeat interval now.

To solve the page refresh problem, I save a reference to the threads based on Session ID, so before creating a new thread I can kill of any existing thread. (It doesn’t work for multiple UIs as it kills the thread for all UIs, but I’ll come back to that :slight_smile: )

Thanks for your help.

Stuart.

see https://vaadin.com/forum/thread/17228199