Register view in Navigator (addView)


I’ve seen in several places now that the view must be registered using the method addView in the Navigator. I haven’t done that in my application and it works perfectly. What is addView for?


When you add your Views using addView the Navigator will automatically set the Uri Fragments to the respective View. This way you don’t need any kind of listener or manual way of handling the UriFragment change event and the view change.
Book of Vaadin 7 - Navigator

Navigator also calls View.enter and ViewChangeListeners, handles nonexistent views etc… Views are useless without a Navigator, if you don’t need Navigator-based navigation you don’t need Views.

We need navigation and it’s navigating well. The thing is that I’ve inherited the code of the application. I’ve been now taking a look and what we have is a class that extends DiscoveryNavigator and implements ViewChangeListener.
Maybe I’m very lost here, but maybe the correct way to do it is to not implements the ViewChangeListener but register the views?

Hmm, yes… Navigator is responsible for changing the views, you don’t need to do it yourself! ViewChangeListeners are for the use cases where you want to do some extra actions such as authorization when the view changes.

I see… what problems can I get by leaving it like this? performance? I think it’s quite nice to not have to register every single view! Maybe my predecessors made it on purpose…

Any thoughts about this?


It’s not clear to me from the above discussion how you use the ViewChangeListener. As far as I know, a ViewChangeEvent can only occur for a view registered with addView() or created with a ViewProvider.

It’s better at least for memory usage and probably also for performance not to create and register all views at once during initialization. You can create views dynamically with a ViewProvider, but you’re not using it if I understood correctly?

Ohhh! I see now with your explanation what is AddView for. We are using Spring and our views are autowired, so we don’t need to create them.
I guess everything is in order then, I just started wondering about this because when I access the website the view is called two times (it’s possible to go back in the navigator and then an exception occurs). There seems to be a workaround solution (!/thread/3395652) but it’s not working for me, so I thought something was wrong with our setup. But I’ll ask in that thread then.

Thanks for help