Now that App.getInstance().navigateTo is deprecated how do I navigate to a screen

I am trying to setup Screen Links in my project and have created a class that extends LinkHandler.

Inside my class I have handle() method with the following code to navigate to a specified screen:

app.navigateTo("registration");

However, it indicates that the navigateTo method is now deprecated.
What code can I therefore use to open or navigate to a screen ?

It is clearly stated in the JavaDoc of navigateTo:

    /**
     * Initialize new TopLevelWindow and replace current.
     *
     * @param topLevelWindowId target top level window id
     * @deprecated Use {@link Screens#create(Class, Screens.LaunchMode)} with {@link OpenMode#ROOT}
     */

You may use Ctrl+Q shortcut to see JavaDoc when your cursor is set on navigateTo call:
image

So, this should help:

@Inject 
private Screens screens;

screens.create(MyTopLevelScreen.class, OpenMode.ROOT).show()

Or with ID:

Screens.create("registration", OpenMode.ROOT).show()
1 Like

Thank you for the response. I am now receiving the following error when I try that:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cuba_LinkHandler': Unsatisfied dependency expressed through field 'screens'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.haulmont.cuba.gui.Screens' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}

Here is my code:

package com.company.kmdclient.web;

import com.company.kmdclient.web.screens.registration.RegistrationPublic;
import com.haulmont.cuba.gui.ComponentsHelper;
import com.haulmont.cuba.gui.Screens;
import com.haulmont.cuba.gui.WindowManagerProvider;
import com.haulmont.cuba.gui.config.WindowConfig;
import com.haulmont.cuba.gui.screen.OpenMode;
import com.haulmont.cuba.gui.screen.Screen;
import com.haulmont.cuba.web.App;
import com.haulmont.cuba.web.AppUI;
import com.haulmont.cuba.web.sys.LinkHandler;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinService;
import com.vaadin.server.WrappedSession;

import javax.inject.Inject;
import java.util.Collections;
import java.util.Map;



public class KmdclientLinkHandler extends LinkHandler {
    @Inject
    private Screens screens;

    public KmdclientLinkHandler(App app, String action, Map<String, String> requestParams) {
        super(app, action, requestParams);
    }

    @Override
    public boolean canHandleLink() {
        if ("register".equals(action)) {
            return true;
        }

        return super.canHandleLink();
    }

    @Override
    public void handle() {

        if ("register".equals(action)) {
            try {

                // open custom main window
                //app.navigateTo("registration");
                screens.create("registration", OpenMode.ROOT).show();
                action = null;
                requestParams.clear();
                // init with request params
                //RegistrationPublic registrationPublic = (RegistrationPublic) app.getTopLevelWindow();
                //registrationPublic.setup(requestParams != null ? requestParams : Collections.emptyMap());
            } finally {
                VaadinRequest request = VaadinService.getCurrentRequest();
                WrappedSession wrappedSession = request.getWrappedSession();
                wrappedSession.removeAttribute(AppUI.LAST_REQUEST_PARAMS_ATTR);
                wrappedSession.removeAttribute(AppUI.LAST_REQUEST_ACTION_ATTR);
            }
        } else {
            super.handle();
        }
    }
}

Solved:

Because I am extending LinkHandler which apparently is not at the presentation level, I cannot @Inject Screens. I had to use AppUI.getCurrent().getScreens() instead as follows:

RegistrationPublic screen =  AppUI.getCurrent().getScreens().create(RegistrationPublic.class, OpenMode.ROOT);
                screen.setup(requestParams);
                screen.show();
1 Like