How to update tab text in app layout dynamically

Hi

I have an appLayout which is references in view classes, layout = MainLayout.class
I need to change tab text dynamically. I set tab.getElement().setText(name); in afterNavigation() inside the layout class (mainLayout.class). It works fine on the first load, but somehow prevent navigation after that. How to make this work, is there some better way without loop:

public static void setTabName() {

	User user = (User) Utils.getUser();
	
	if(myiUser!=null) {
    	for(int i=0;i<menu.getComponentCount();i++) {
    		Component component=menu.getComponentAt(i);
    		
    		if(component instanceof Tab) {
        		Tab tab=(Tab)component;
        		Optional<String> id=tab.getId();
        		if(id.isPresent() && id.get().equals(ViewConstant.VIEW_NAME.toString())) {
        			String text=user.getText();
        			tab.getElement().setText(text);
        			
    			}
    		}
    	}
	}

}

Hi,

What do you mean by

but somehow prevent navigation after that.

?

Based on the code snippet, I’m not sure what could break navigation there. One weird thing I see in your code is that you’re using a static method. You should not store references to Vaadin Components in static context. Each attached Component is bound to a specific UI (and Session); you can’t share Component instances between UIs.

Anyway, if you can create a more complete [minimal reproducible example]
(https://stackoverflow.com/help/minimal-reproducible-example), it would be easier to analyze what could be going wrong.

Hi.

Actually I found out what the issue was. It was an issue with UI instance bounding and a small bug in:

tab.getElement().setText(text);
→ should be
((RouterLink) child).setText(networkName);

Thanks for that tip about UI and session context.