Strange behavior of Navigation

Hi,

i am trying to write a vaadin Application. But i am stucked with the first Problem.
I want to have a Navigation and a Content Area on my Application.

If i click something on the Navigation, only the Contentarea should change => Sounds genius :slight_smile:
Therefore i read the wiki and found this:
https://vaadin.com/book/vaadin7/-/page/advanced.navigator.html

I modified the code and got a really strange behavior, that i dont undestand. I added a Button, whats adds a new Button to the Navigation. (I only did this, to verify, that only the content area reloads by clicking on the navigation buttons).
But: When i go on the Page the First time, and i click the test button, then a new test button appears. If i now click on View1, the button disapears.
Until this moment, i pressed one of the Buttons View1 or View2 and i add now a test Button via the Button “test”, and i click again an View1 oder View2 the button dont disapear.
I hope i described my problem well enough for understanding.

Here is my code:

public class MainView  extends VerticalLayout implements View {
    public static final String NAME = "";
	private Panel panel;
    // Menu navigation button listener
    class ButtonListener implements Button.ClickListener {
        String menuitem;
        public ButtonListener(String menuitem) {
            this.menuitem = menuitem;
        }
        @Override
        public void buttonClick(ClickEvent event) {
            // Navigate to a specific state
        	getUI().getNavigator().navigateTo("main" + "/" + menuitem);
        }
    }

    public MainView() {
        setSizeFull();
        
        // Layout with menu on left and view area on right
        HorizontalLayout hLayout = new HorizontalLayout();
        hLayout.setSizeFull();
        
        // Panel for the navgigation
        Panel menu = new Panel("Navigation");
        menu.setHeight("100%");
        menu.setWidth(null);
        
        //creating the navigationcontent
        final VerticalLayout menuContent = new VerticalLayout();
        menuContent.addComponent(new Button("View1",
                  new ButtonListener("View1")));
        menuContent.addComponent(new Button("View2",
                  new ButtonListener("View2")));
        menuContent.setWidth(null);
        menuContent.setMargin(true);
        menu.setContent(menuContent);
        
        //Panel dem Layout hinzufügen
        hLayout.addComponent(menu);
        
        // A panel that contains a content area on right
        panel = new Panel("Inhalt");
        panel.setSizeFull();
        hLayout.addComponent(panel);
        hLayout.setExpandRatio(panel, 1.0f);

        addComponent(hLayout);
        setExpandRatio(hLayout, 1.0f);
        
[b]
        //create new test navigation button
        Button test = new Button("test", new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
            	menuContent.addComponent(new Button("test"));
            }
        });
        menuContent.addComponent(test);
[/b]
    }        
    
    @Override
    public void enter(ViewChangeEvent event) {
        VerticalLayout panelContent = new VerticalLayout();
        panelContent.setSizeFull();
        panelContent.setMargin(true);
        panel.setContent(panelContent); // Also clears

        //Standardcontent anzeigen
        if (event.getParameters() == null
            || event.getParameters().isEmpty()) {
            panelContent.addComponent( new Label("DefaultPage"));
            return;
        }
        // Display the fragment parameters
        Label watching = new Label(
            "You are currently watching a " + event.getParameters());
        watching.setSizeUndefined();
        panelContent.addComponent(watching);
        panelContent.setComponentAlignment(watching,
            Alignment.MIDDLE_CENTER);
        
    }
}

The question is why this is happening and the next question is should i do my navigation like this.
I am reading about use a differnt View for each page, but in my understanding, if i am doing this, this way. I only have one View which loads different CustomComponents to my ContentArea.

Thanx for Helping