Navigation Lifecycle

The navigation lifecycle is made up of a number of events that are fired when a user navigates in an application from one state or view to another.

The events are fired to listeners added to the UI instance and to attached components that implement related observer interfaces.

BeforeLeaveEvent

BeforeLeaveEvent is the first event fired during navigation.

The event allows the navigation to be postponed, canceled, or changed to a different destination.

This event is delivered to any component instance implementing BeforeLeaveObserver that is attached to the UI before the navigation starts.

It is also possible to register a standalone listener for this event using the addBeforeLeaveListener(BeforeLeaveListener) method in UI.

A typical use case for this event is to ask the user whether they want to save any unsaved changes before navigating to another part of the application.

Postpone method

BeforeLeaveEvent includes the postpone method that can be used to postpone the current navigational transition until a specific condition is met.

Example: The client requests the user’s confirmation before leaving the page:

Java

  1. public class SignupForm extends Div
  2. implements BeforeLeaveObserver {
  3. @Override
  4. public void beforeLeave(BeforeLeaveEvent event) {
  5. if (this.hasChanges()) {
  6. ContinueNavigationAction action =
  7. event.postpone();
  8. ConfirmDialog.build("Are you sure you want"+
  9. " to leave this page?")
  10. .ifAccept(action::proceed)
  11. .show();
  12. }
  13. }
  14. private boolean hasChanges() {
  15. // no-op implementation
  16. return true;
  17. }
  18. }

Postponing interrupts the process of notifying observers and listeners. When the transition resumes, the remaining observers (those after the observer that initiated the postpone) are called.

Example:

  • Assume the current page has 3 observers, a, b and c, which are notified in the same order.

  • If b calls postpone, the call to c (and the rest of the transition process), is deferred.

    • If the transition is not resumed, c is never notified of the event and the transition never finishes.

    • If b executes ContinueNavigationAction to resume the transition, it continues from the point of interruption: a and b are not called again, but c is notified.

Note
Only one navigation event may be postponed at a time. Starting a new navigation transition, while a previous one is in a postponed state, makes the postponed state obsolete, and executing ContinueNavigationAction has no effect.

BeforeEnterEvent

BeforeEnterEvent is the second event fired during navigation.

The event allows you to change the navigation to go to a destination that is different from the original.

This event is typically used to react to special situations, for example if there is no data to show, or if the user does not have appropriate permissions.

This event is delivered to any component instance implementing BeforeEnterObserver that is attached to the UI starting from the UI moving through the child components.

Note
Components in the chain are instantiated after a parent handles BeforeEnter and doesn’t reroute or forward

The event is fired:

  • Only after a postpone (called during a BeforeLeaveEvent) has been continued.

  • Before detaching and attaching components to make the UI match the location being navigated to.

It is also possible to register a standalone listener for this event using the addBeforeEnterListener(BeforeEnterListener) method in UI.

Rerouting

Both BeforeLeaveEvent and BeforeEnterEvent can be used to reroute dynamically.

Rerouting is typically used when there is a need to show completely different information in a particular state.

When the reroute method is called:

  • The event is not fired to any further listeners or observers.

  • The method triggers a new navigation phase, based on the new navigation target, and events are fired based on this instead.

  • For BeforeEnterEvent: Child components of the rerouting component are not instantiated.

Example: Rerouting when entering a BlogList with no results.

Java

  1. @Route("no-items")
  2. public class NoItemsView extends Div {
  3. public NoItemsView() {
  4. setText("No items found.");
  5. }
  6. }
  7. @Route("blog")
  8. public class BlogList extends Div
  9. implements BeforeEnterObserver {
  10. @Override
  11. public void beforeEnter(BeforeEnterEvent event) {
  12. // implementation omitted
  13. Object record = getItem();
  14. if (record == null) {
  15. event.rerouteTo(NoItemsView.class);
  16. }
  17. }
  18. private Object getItem() {
  19. // no-op implementation
  20. return null;
  21. }
  22. }
Note
There are several rerouteTo overload methods that can be used for different use cases.

Forward

The forwardTo method reroutes navigation and updates the browser URL.

Forwarding can be used during BeforeEnter and BeforeLeave lifecycle states to dynamically redirect to a different URL.

When the forwardTo method is called:

  • The event is not fired to any further listeners or observers.

  • The method triggers a new navigation phase, based on the new navigation target, and fires new lifecycle events for the new forward navigation target.

  • For BeforeEnterEvent: Child components of the forwarding component are not instantiated.

Example: Forwarding when viewing BlogList without the required permissions.

Java

  1. @Route("no-permission")
  2. public class NoPermission extends Div {
  3. public NoPermission() {
  4. setText("No permission.");
  5. }
  6. }
  7. @Route("blog-post")
  8. public class BlogPost extends Div
  9. implements BeforeEnterObserver {
  10. @Override
  11. public void beforeEnter(BeforeEnterEvent event) {
  12. if (!hasPermission()) {
  13. event.forwardTo(NoPermission.class);
  14. }
  15. }
  16. private boolean hasPermission() {
  17. // no-op implementation
  18. return false;
  19. }
  20. }
Note
forwardTo has several overloads that serve different use cases.

AfterNavigationEvent

AfterNavigationEvent is the third and last event fired during navigation.

This event is typically used to update various parts of the UI after the actual navigation is complete. Examples include adjusting the content of a breadcrumb component and visually marking the active menu item as active.

The event is fired:

  • After BeforeEnterEvent, and

  • After updating which components are attached to the UI.

At this point, the current navigation state is actually shown to the user, and further reroutes and similar changes are no longer possible.

The event is delivered to any component instance implementing AfterNavigationObserver that is attached after completing the navigation.

It is also possible to register a standalone listener for this event using the addAfterNavigationListener(AfterNavigationListener) method in UI.

Example: Marking the active navigation element as active.

Java

  1. public class SideMenu extends Div
  2. implements AfterNavigationObserver {
  3. Anchor blog = new Anchor("blog", "Blog");
  4. @Override
  5. public void afterNavigation(
  6. AfterNavigationEvent event) {
  7. boolean active = event.getLocation()
  8. .getFirstSegment()
  9. .equals(blog.getHref());
  10. blog.getElement()
  11. .getClassList()
  12. .set("active", active);
  13. }
  14. }