How do I preserve a Vaadin view instance when navigating between views

Viewed 121

I have a view which is quite heavy with data. The data is loaded when the view is initialized. Now, when I navigate away from the view using Router and then return to the view, a new view instance is created triggering refetching of all the data. I would like to preserve the original view instance even when returning to the view - how can this be achieved?

Edit: To add details, this is a Spring Boot application and unlike to my expectation, annotating a view as @UIScope didn't keep the instance, but a new instance was created every time when navigating to the view.

1 Answers

Answering my own question, in case someone else runs into the same situation.

I had a Spring Boot application, so the first step was to define the scope for the views so that they wouldn't be recreated, a suitable scope is UIScope. However, that is not enough, even though injecting of beans work out-of-the-box, the views don't seem to be managed beans by default, so adding @Component annotation to the view makes them managed and thus, the @UIScope scoping also starts working.

@UIScope
@PreserveOnRefresh
@PageTitle("example")
@Route(value = "example", layout = MainLayout.class)
@Component
public class ExampleView extends VerticalLayout {
Related