Redirecting from one page to another without an event

Hi I have a view, I’m trying to implement a redirection after 5 seconds, my ScheduledExecuterService works fine, but the redirecion is not working.

@Route(value = “result”)
@Uses(Icon.class)
public class ResultView extends VerticalLayout implements BeforeEnterObserver {

private ScheduledExecutorService executorService;

public ResultView() {
    Hr hr1 = new Hr();
    add(hr1);
    Image image = new Image("images/logo.png", "Logo CORFAMET");

    add(image);
}

@Override
public void beforeEnter(BeforeEnterEvent event) {
    String descripcion = event.getLocation().getQueryParameters().getParameters().get("descripcion").get(0);
    String precio = event.getLocation().getQueryParameters().getParameters().get("precio").get(0);
    Hr hr2 = new Hr();
    add(hr2);

    Span spanDescription = new Span();
    spanDescription.getElement().getStyle().set("font-size", "4em");
    add(spanDescription);

    Hr hr3 = new Hr();
    add(hr3);

    Span spanPrice = new Span("S/ 2.30");
    spanPrice.getElement().getStyle().set("font-size", "8em");
    add(spanPrice);

    spanDescription.setText(descripcion);
    spanPrice.setText(precio);

    Button homeButton = new Button("Regresar");
    homeButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY,
            ButtonVariant.LUMO_ERROR, ButtonVariant.LUMO_LARGE);
    homeButton.setHeight("150px");
    homeButton.setWidth("300px");
    homeButton.getStyle().set("font-size", "50px");
    homeButton.addClickListener(eventb -> getUI().ifPresent(ui -> ui.navigate("")));
    add(homeButton);

    executorService = Executors.newSingleThreadScheduledExecutor();
    executorService.scheduleAtFixedRate(() -> {
        System.out.println("Hi");
        UI ui = UI.getCurrent();
        ui.access(() -> ui.navigate(""));
    }, 5, 5, TimeUnit.SECONDS);
}

}

The obvious question: did you add the Push annotation?

And you should pass the UI instance go your scheduler, it’s normally not available by calling getCurrent in another thread

Oh my fault, I added the push annotation and I change the implementation and now It’s working. Thanks @quirky-zebra
@Override
protected void onAttach(AttachEvent attachEvent) {
thread = new FeederThread(attachEvent.getUI(), this);
thread.start();
}

@Override
protected void onDetach(DetachEvent detachEvent) {
    // Cleanup
    thread.interrupt();
    thread = null;
}

private static class FeederThread extends Thread {
private final UI ui;
private final ResultView view;

    public FeederThread(UI ui, ResultView view) {
        this.ui = ui;
        this.view = view;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(3500);
            ui.access(() -> ui.navigate(""));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}