Navigator Lifecycle and exit method

HI all,

I was wondering if there is something like an on exit Method when the View is changed in the Navigator.
Before the View is displayed the enter Method is executet, so I wonder what happens before the change.
Is the old View destroied or is it kept alive?

If the view is kept alive when are the views created and when destroid? Only as needed or at the beginning of a Vaadin application?
If it is relevant I use Vaadin 7.0.1

I would like to remove some Listeners before the View changes or at least before it is destroid.

And does



for the events, you can call Navigator.addViewChangeListener(…) to add a listener that will be notified both before and after a view change. The event ifself includes references to e.g. the ‘old’ and ‘new’ views.

The view life cycle depends on how you add it. If you use the addView(String viewName, View view) method the same view instance will be alive and returned for the lifetime of the Navigator. If you use the addView(String viewName, Class<? extends View> viewClass) method, the Navigator will create a new instance of he view every time you enter it, and old instances are discarded as they’re navigated away from.

For more customization, you can also write your own class implementing the ViewProvider interface, giving you more control over the view creation.

Hi Teppo,

thanks for the advice. The Listener is exactly what I need.
As I already use the addView(String viewName, Class<? extends View> viewClass) to register my views I don’t think I will need an own ViewProvider.
But nevertheless I will have a closer look at it.


Hi again,

when using the ViewChangeEvent i have a befor and after change View method.

If I now figure out that the User has unsaved changes and want to ask him if he wants to proced without saving or stay on the page. How can I do this.
I think it is not possible to wait here for the answer of the user, that correct?
So I only can check if he has unsaved changes and send him a notification that he can not change the view if it is not saved yet.

Anything I am missing there?



you’re right, you can not wait at this point (as in a blocking wait). But what you can do, is that you can return true from the beforeViewChange method, which will prevent the navigation and stay in the old view. Be sure to store the name (and parameters) of the view the navigation was going to end up (you get these from the event too). Then you can show a save dialog or whatever, and after the user has responded, you can ask the Navigator to navigate to the new view using the view name and parameters you stored previously.

There is also a
related to this.

sorry for the late reply, somehow my subscribtion to the thread did not notify my that a new answer was postet…

The Tutorial is more or less what I was locking for.

Thanks for the Help

This feature is included in

Just use
instead of
instead of
. (both are drop-in replacements for their Vaadin core counterparts)

See the Javadoc for
and you’ll understand. :slight_smile: