Re-using instances of views

Hi There,
I’m looking for a way to be able to re-use instances of views, to achieve the following: Suppose the user navigates from one view to another, then hits the ‘back’ button, the user might expect to return in the same view as before, so still having e.g. the same grid-rows selected, buttons selected etc.
I’d hoped that the Bakery example behaved in that way, but just found out it does not. This is the piece of code (from SpringVoewProvider) that we see creating a new instance of a view every time (even withing the same session), where we’d hope that the same view instance would be returned (for the same session):

    protected View getViewFromApplicationContext(ViewInfo viewInfo) {
        View view = null;
        if (isAccessGranted(viewInfo)) {
            final BeanDefinition beanDefinition = getBeanDefinitionRegistry()
                    .getBeanDefinition(viewInfo.getBeanName());
            if (beanDefinition.getScope()
                    .equals(ViewScopeImpl.VAADIN_VIEW_SCOPE_NAME)) {
                LOGGER.trace("View [{}]
 is view scoped, activating scope",
                        viewInfo.getViewName());
                final ViewCache viewCache = ViewScopeImpl
                        .getViewCacheRetrievalStrategy()
                        .getViewCache(getWebApplicationContext());
                viewCache.creatingView(viewInfo.getViewName());
                try {
                    view = getViewFromApplicationContextAndCheckAccess(
                            viewInfo);
                } finally {
                    viewCache.viewCreated(viewInfo.getViewName(), view);
                }
            } else {
                // view scope is not active for non-view-scope views as we don't
                // hook into their lifecycle
                view = getViewFromApplicationContextAndCheckAccess(viewInfo);
            }
        }
        return view;
    }

“Re-using views” is not a correct term in your case, I understand your question that you want to implement navigation history. Vaadin applications are single page, hence by default navigation history does not work as it would work on a multipage website. However it is possible to have navigation history e.g. via Push State navigation, see more here. Note however, that does not imply that same selections, etc. would be preserved.

https://vaadin.com/docs/v8/framework/advanced/advanced-pushstate.html

Thanks Tatu for your answer, but… It raises some more questions.
Sticking to the Bakery example: Multiple views are used (in a single page), is there a real technical reason why the entity of e.g. the UserAdminView cannot be re-used after the user has e.g. visited the ProductAdminView. Is there a technical reason why a new instance has to be created intead of re-using the previous one???
Maybe I don’t understand you remark about single-page application?

And, if there is a real hard reason why a view instance cannot be re-used, then how would I implement a “Wizzard”?
(a wizzard with multiple steps the user can walk throug, with previous and next buttons) I planned for the Wizzard to be built from multiple (re-usable) instances of views (for every step of the wizzard a specific view). So that, if the user decided to go back in the wizzard, his selections/choices are preserved.

Is there another preffered way of handling such preservation (and not preserving in the database)?

Ok, I am starting to understand what you are trying to achieve.

You are using Spring, and that will be the answer to your problem.

I would bundle the content that you want to “re-use” as custom component or composite as Spring managed Bean, which you can then Autowire to your View. You probably want to make it UIScoped, since then Bean manager will instantiate it only once, and after that when you re-enter the view, Bean manager will serve the same instance of the Bean. That way the state of that can be preserved despite navigation.