Bug in navigation

    public static final String CALENDAR_VIEW = "calendar";
    public static final String WOD_VIEW = "wod";
@Tag("calendar-view")
@Route(value = CALENDAR_VIEW, layout = MainLayout.class)
@PageTitle("Calendar")
@SpringComponent
@UIScope
public class CalendarView extends VerticalLayout implements AfterNavigationObserver {
//implementation omitted
	@Override
    public void afterNavigation(AfterNavigationEvent event) {
        System.out.println("afterNavigation called in CalendarView");
	}
}
@Tag("wod-view")
@Route(value = WOD_VIEW, layout = MainLayout.class)
@PageTitle("WOD")
@SpringComponent
@UIScope
class WodView extends VerticalLayout implements AfterNavigationObserver {
//implementation omitted
	@Override
    public void afterNavigation(AfterNavigationEvent event) {
        System.out.println("afterNavigation called in WodView");
	}
}
@Route(value = "")
@PageTitle("Login")
@CssImport("./styles/login-view.css")
@Viewport(UiConstants.VIEWPORT)
class LoginView extends Div {
	private void loginClickListener() {
		UI.getCurrent().navigate(WodView.class);
	}
}
public class MainLayout extends AppLayout {
	MainLayout(@Autowired WodView wodView, @Autowired CalendarView calendarView) {
		MenuBar menuBar = new MenuBar();
		menuBar.addItem("WOD", e -> setContent(wodView));
		MenuItem admin = menuBar.addItem("Admin");
        SubMenu adminSubMenu = admin.getSubMenu();
		adminSubMenu.addItem("Calendar", e -> setContent(calendarView));
	}
}

On login, I’m automatically taken to the WodView by the loginClickListener. From there, if I click the ‘Calendar’ link in the menu bar, I’m taken to the CalendarView. The CalendarView displays correctly. However, the URL in the address bar never changes from localhost:8080/wod to localhost:8080/calendar as it should. In addition, the afterNavigation method in CalendarView never gets called. As a matter of fact, if you go to CalendarView, the afterNavigation method in WodView gets called again. Unless I’m mistaken, all of my code is implemented exactly as directed by the Vaadin documentation.

Hi,
Instead of using setcontent(calendarView) in the menu, you can use navigate(CalendarView.class). it will update the url and navigate ( change the content of the main layout, trigger the navigation events and show the calendar).

    public static final String CALENDAR_VIEW = "calendar";
    public static final String WOD_VIEW = "wod";
@Tag("calendar-view")
@Route(value = CALENDAR_VIEW, layout = MainLayout.class)
@PageTitle("Calendar")
@SpringComponent
@UIScope
public class CalendarView extends VerticalLayout implements AfterNavigationObserver {
//implementation omitted
	@Override
    public void afterNavigation(AfterNavigationEvent event) {
        System.out.println("afterNavigation called in CalendarView");
	}
}
@Tag("wod-view")
@Route(value = WOD_VIEW, layout = MainLayout.class)
@PageTitle("WOD")
@SpringComponent
@UIScope
class WodView extends VerticalLayout implements AfterNavigationObserver {
//implementation omitted
	@Override
    public void afterNavigation(AfterNavigationEvent event) {
        System.out.println("afterNavigation called in WodView");
	}
}
@Route(value = "")
@PageTitle("Login")
@CssImport("./styles/login-view.css")
@Viewport(UiConstants.VIEWPORT)
class LoginView extends Div {
	private void loginClickListener() {
		UI.getCurrent().navigate(WodView.class);
	}
}
public class MainLayout extends AppLayout {
	MainLayout(@Autowired WodView wodView, @Autowired CalendarView calendarView) {
		MenuBar menuBar = new MenuBar();
		menuBar.addItem("WOD", e -> setContent(wodView));
		MenuItem admin = menuBar.addItem("Admin");
        SubMenu adminSubMenu = admin.getSubMenu();
		adminSubMenu.addItem("Calendar", e -> setContent(calendarView));
	}
}

On login, I’m automatically taken to the WodView by the loginClickListener. From there, if I click the ‘Calendar’ link in the menu bar, I’m taken to the CalendarView. The CalendarView displays correctly. However, the URL in the address bar never changes from localhost:8080/wod to localhost:8080/calendar as it should. In addition, the afterNavigation method in CalendarView never gets called. As a matter of fact, if you go to CalendarView, the afterNavigation method in WodView gets called again. Unless I’m mistaken, all of my code is implemented exactly as directed by the Vaadin documentation.

Thank you! You guys should add that to the documentation.

One last question… it’s a little counterintuitive that a method named “afterNavigation” would ever get called before navigation, but it does. Is there a something view classes could listen for to know when session variables are available? The Bakery example uses Spring Security to solve this particular issue, but it would be nice if there was an alternative.

 @Override
    public void afterNavigation(AfterNavigationEvent afterNavigationEvent) {
        User currentUser = ((User)VaadinSession.getCurrent().getAttribute(CURRENT_USER));
        if (currentUser != null) {
            schedule.setVisible(currentUser.getRole() == ADMIN);
        }
    }