In Vaadin I want to show a Notification after a page reload:
UI.getCurrent().getPage().reload();
new Notification("Success", 5000).open();
But with this attempt no notification is shown. What is the problem here and how do I fix it?
Although it looks very imperative to show a notificiation, this still is part of the state of UI; so when you reload, you are basically (re-)creating a fresh state, where the notification is not part of.
You have to signal to the "new" UI, that you want something to be done.
One way to do this is by storing a marker inside the session (where all UIs are part of). Then use a observer on a component (or a listener for your whole application) for when the navigation is done, to resolve the notification.
E.g.
@Route(value = "", layout = MainLayout)
@SuppressWarnings('unused')
class MainView extends Composite<Div> implements AfterNavigationObserver {
public static final String RELOADED_KEY = 'reloaded'
MainView() {
content.add(
new Button("Reload and notify", {
getUI().get().tap {
getSession().setAttribute(RELOADED_KEY, true)
getPage().reload()
}
})
)
}
@Override
void afterNavigation(AfterNavigationEvent afterNavigationEvent) {
def session = getUI().get().getSession()
if (session.getAttribute(RELOADED_KEY)) {
session.setAttribute(RELOADED_KEY, false)
Notification.show('Reloaded...')
}
}
}