One can also make the View itself trigger a navigation to another View in navigateTo(), but let’s take a look at the more flexible beforeViewChange() and afterViewChange(), that exists specifically for this purpose.

    First, let’s continue from previous examples and create a MessageView for secret messages:

    Java

    Next, we’ll register this new View with the Navigator, exactly as before. At this point our SecretView is not secret at all, but let’s fix that by adding a ViewChangeListener to the Navigator:

    Java

    So if we’re on our way to the SecretView, but not logged in (getLoggedInUser() == null), the View change is cancelled. Quite simple rules in our case, but you could check anything - most probably you’ll want to call a helper method that checks the user for permission.

    Java

    Instead of just showing a notification and leaving the user wondering, we should obviously allow the user to log in and continue. We’ll do just that in the separate tutorial about Handling login, but for now we just add a button that toggles our logged in/out state.

    Meanwhile, here is the the full source for the UI so far: