Reusing component instances

Vaadin noob here. (Old gwt user.) Playing around with the system, I am following the basic demo of the AppLayout with a couple of Views (About and something else). I notice that the system instantiates a new View instance every time the user navigates between Views. Fine for a demo but in practice I’d want to reuse my View instances. Maintaining the position of the slider for a SliderLayout, or the contents and current expansion of a populated TreeGrid, for example.

@PreserveOnRefresh preserves the instance when doing a browser refresh, but it doesn’t reuse the instance when navigating.

You can play around with different route or UI scopes for the view. Keep in mind that this adds complexity on your Implementation part.

Ah. At https://vaadin.com/docs/latest/integrations/spring/scopes it says:

If no scope is specified in addition to the @SpringComponent / @Component annotation, the component is in singleton scope.

and

Without any annotations, the components act like a bean in prototype scope.

The latter was my issue.

Interestingly, using the TreeGrid with a SplitLayout, the TreeGrid remembers its state, but the SplitLayout does not remember its drag position.

There are the little inconveniences :sweat_smile: even tho I’m not sure if that’s intended or a bug - cc @useful-whale

no idea really, such an old implementation. Tickets are welcome of course https://github.com/vaadin/web-components/issues/new/choose

@PreserveOnRefresh
This does what the name implies, i.e. preserves the view on refresh. It is not PreserveOnNavigation.

Instead of scoping your components with odd scopes, you should back your view data bean, with suitable scope, and populate your view data from that bean in afterNavigation of the route. Say, if your bean is VaadinSessionScoped, it will cache the data while you navigate, as VaadinSessionScope is preserved over navigation. Note, UIScope is not preserved on Refresh as rereshing the page will create new UI and @PreserveOnRefresh just copies the content from old UI.