vertical Scroll position remains after Page routing

Hi, how can I set the scroll position to top after each call of UI.getCurrent().navigate("PAGE_ROUTE");?

You could e.g. call focus and scrollIntoView on the first component in the new view

Sounds not really intuitive. The default behavior of not setting the scroll position to the begging after navigation seems like a bug to me. Isn’t there a better fix than calling js code ?

Those are methods available in java

Oh ok. And is it possible to do not repeat the code again and again ?

Inheritance or using a generic after view change listener that looks for the first component and focuses it.

I guess inheritance wont do it because I can only extend a class only once (e.g. extends VerticalLayout). Can you give some detailed hints ? I’m a bit struggling how to implement your mentioned approaches.

Implementing AfterNavigationObserver and overriding the method afterNavigation works. But as I mentioned it’s repetitive. I have 50+ Views lol.

    @Override
    public void afterNavigation(AfterNavigationEvent event) {
        scrollIntoView();
    }

Ok I think I’ve solved it :slightly_smiling_face: Instead of implementing the AfterNavigationObserver I implemented the AfterNavigationListener in the class responsible for the main layout.

// attach the listener
    @Override
    protected void onAttach(AttachEvent attachEvent) {
        getUI().ifPresent(ui -> ui.addAfterNavigationListener(this));
    }

    @Override
    public void afterNavigation(AfterNavigationEvent event) {
        // UI.getCurrent().scrollIntoView(); => won't work
        //UI.getCurrent().getPage().executeJs("window.scrollTo(0,0);"); => won't work
        getContent().scrollIntoView(); // => This works ! :)
    }