Navigating back to previous view instance?

When navigating, can I navigate to a specific instance of a view?
Failing that, can I fake it?

I have a sidebar that shows actions the user is expected to respond to.
Clicking on these actions uses regular navigation to go to various action-screens, and when user is done, he or she clicks on our (not browser’s) “back” button to go back to the screen instance they started in, with all (server-side) state exactly as they left it.

I can remember the old view and restore it.
I can restore the title
But, can I restore the url?

Yes, you have to save the view state yourself.
When reading your post I think about some things:

  • UI.navigate(Class<Component>) returns an optional with the view instance. There you could pass a state or the result of the action.
  • Save the state by a key from a query parameter. You could save the state with that key and in combination with session id or window name. Like www.example.com/yourview?state=some-uuid-123
  • You could save it in the Vaadin session: VaadinSession.getCurrent().setAttribute(key, value).
  • And you would save the current URL within that state and then you can update the URL with History.replaceState, see docs.

That was the reference I needed. The rest of the state is just the view instance.
So, I ended up with something like this in my mainLayout class:

// Before navigating to targetScreen, set up a button to return to the current screen

var pushedScreen = getCurrentScreen();

var revertButton = new Button("Revert");
toolbar.add(revertButton);

revertButton.addClickListener( () -> {

    toolbar.remove(revertButton);

    var currentScreen = getCurrentScreen();
    replace(currentScreen, pushedScreen);

    var page = UI.getCurrent().getPage();
    page.setTitle(pushedScreen.getTitle());

    var url = RouteConfiguration.forSessionScope().getUrl(pushedScreen.getClass());
    page.getHistory().replaceState(null, url);

});

// Navigate to targetScreen

UI.getCurrent().navigate(targetScreenClass);

1 Like