my app is similar to the starter apps: AppLayout, SideNav with some entries pointing to some views.
I’d like to keep the state of the views when navigating from one to another, e. g. by clicking on the menu entries.
However, the views are always newly created, even when adding @PreserveOnRefresh.
PreserveOnRefresh comes only into play, when reloading the page itself. It has no effect, when navigating between two views using links or UI navigate.
To reuse existing components, you have to cache them somewhere or use Spring Beans with the @UIScope annotation.
But please note, that this may lead to memory issues, when having a lot of users or long living UIs. Also not every component state is synchronized with the server, so for instance the Grid scroll position will be resetted on each view change.
As far as I understand it RouteScope can be applied to beans which are injected to the routing component, but not to that component (i. e. the view) itself…?
I also have to add @SpringComponent in addition to @UIScope, then it works. (I was under the impression that @Route implicitly makes it a Spring component, but I was obviously wrong.)
Vaadin’s Router will instantiate views / routes using Spring instantiator, which allows you to use autowiring with them, but they are not beans fully. So if you want to have full bean nature the @SpringComponent annotation is required. E.g. that will allow to use @PostContruct, @PreDestroy, etc.