Know which view was the current request routed to

Hi.

I defined a AViewHost class as the routing entry-point for the application, as follows:

@Route(value = "")
public class AViewHost extends Div implements ErrorHandler, PageConfigurator {
[...]

From here, I attach subviews showing the content.

[...]

this.add(new MySubView());
[...]

I need MySubView’s constructor code to be aware of the parent view (instance of AViewHost) it will be attached to.
How can I do that, without passing this when creating MySubView (new MySubView(this))?

In other words, is there any way in Vaadin to know which view was the current request routed to? I tried to look into UI and Router documentation, to no avail.

Thanks,
MZ

This is usually done by [Router Layouts and nested Router Targets]
(https://vaadin.com/docs/v14/flow/routing/tutorial-router-layout.html). The Main/Parent View (in your case AViewHost) does not have a @Route annotation, only the “sub-views” have that. And every sub view defines it’s parent view within the Route annotation using the layout attribute. For this, the parent view needs to implement RouterLayout.

public class AViewHost extends Div implements RouterLayout { 
	...
}


@Route(value = "", layout = AViewHost.class)
public class MySubView extends Div { 
	... 
}

Instead of defining your parent view as the entry-point view of your application, choose a sub view for that. You cannot navigate to a parent view - only to the most low level views (which have their parent views defined).

By default, the sub view will be appended to the div that is your AViewHost. You can change that by overriding showRouterLayoutContent(HasElement content) in the parent view. If the sub-view-content should not be simply appended into the parent-div but instead inserted between some other components, you can define all this within that method override.

Kaspar, thank you very much for your reply, which is very interesting and useful - and possibly SEO-friendly - if you follow a classic approach (1 view = 1 url to route).

Instead, I forgot to add in my original post (my bad, apologies for that…) that I always have 1 real view displayed (AViewHost), attached to “/”, and this view simulates a classic navigation by dynamically modifying its content by attaching/detaching subviews to its main Div’s.
This is because I need AViewHost to keep track of loaded subviews in a stack when I pause something and load another subview (i.e.: show List of invoices > Add new invoice > Add new customer contact, done, then return to Add new invoice, etc.).

However, when initializing a subview, I don’t know who it is attached to, since it still isn’t (this.getParent() == null).

Any way to get to the AViewHost instance from the subview? I tried through Router or RouteRegistry, but they give me the class (AViewHost), not the actual instance.

Thanks a lot,
MZ

I ended up refactoring my code, so this is strictly not necessary anymore.

Still, it could be useful to have such a method, maybe in UI or VaadinRequest.

Thanks,
MZ