possible bug? curious behaviour in UI leads to multiple calls to @PostConst

Hi,

recently i tried to display a modal dialog at the end of the postConstruct method of my view, and i noticed that the dialog was always displayed twice. so apparently the postConstruct method was called twice for some reason.

i placed a break point inside the postConstruct() method and from the stack trace i quickly found the reason for the two calls, but as of now i have no idea behind the intention or how to avoid it. thats what i’m hoping to learn from this thread.

The source of the problem lies in com.vaadin.ui.UI (this is Vaadin 8.8.2 by the way), line 770:

// Call the init overridden by the application developer
init(request);

Navigator navigator = getNavigator();
if (navigator != null) {
	// Kickstart navigation if a navigator was attached in init()
	navigator.navigateTo(navigator.getState());
}

Here the overridden init() method of my custom UI class is called, and inside that init()-method I define a navigator and call navigateTo() on that navigator to go to my starting view. So business as usual, so far. by the way, that call of navigateTo() from within the init() method results in the first call to my postConstruct() method.

But you see from the code snippet of the com.vaadin.ui.UI class that immediately after calling init() the navigator is checked, and if available (which it is in my case) the navigateTo() is called again, which results in the second call to my postConstruct() method.

so now i know WHY it happens, but i still don’t understand why the com.vaadin.ui.UI is structured this way. Why the second call to navigateTo? and how could i avoid this?

my first thought was that instead of calling navigateTo() i would just call setState() from my init() method and then the navigator.navigateTo(navigator.getState()) in com.vaadin.ui.UI would do the actual navigating. but that doesn’t work because of the visibility of the methods that give you access to the state or stateManager. so that doesn’t seem to be an option.

so i’m getting the impression that either there is a possible bug here in com.vaadin.ui.UI or i’m using the whole navigator thing kinda wrong.

either way, i would appreciate some insight and guidance :slight_smile:

thanks in advance!

best regards
Mario

bump