How to detect when one of the main tabs is selected?

Hi all,

I was wondering how I can detect if a screen is selected when it is already opened as a tab? I know there is a tab change event for tab sheets I create on my screen but I can’t find an event or similar to respond to a selection of the main tabs where the screens are opened.

The reason I want to do this is that my main screen of the application contains a dashboard with a table of tasks. These tasks can be generated in different parts of the program where I can’t write something like: findMainScreenAndUpdate()
So I want to reload the task list once a user comes back to the main screen. The onAfterShow event only works when the screen is first opened.

Thanks!

Hi,

AppWorkArea fires WorkAreaTabChangedEvent application event, so you can create a spring bean

@Component
public class WorkAreaTabChangedListener {

    private final ObjectProvider<Screens> screensProvider;

    public WorkAreaTabChangedListener(ObjectProvider<Screens> screensProvider) {
        this.screensProvider = screensProvider;
    }

    @EventListener
    public void onWorkAreaTabChanged(AppWorkAreaImpl.WorkAreaTabChangedEvent event) {
        Screens screens = screensProvider.getIfAvailable();
        if (screens == null) {
            return;
        }

        MainScreen mainScreen = ((MainScreen) screens.getOpenedScreens().getRootScreen());
        // do the needful
    }
}

Regards,
Gleb

1 Like

Hi Gleb,

That is working great, thank you very much.
I was now wondering how to detect if my dashboard is the currently opened tab/screen. I was looking into the thrown event but somehow

event.getSource().getSelectedTab().getCaption()

is returning null.

So for now, I am using this to reload my tasks but I am not sure if this is stable enough?

screens.getOpenedScreens()
                .getCurrentBreadcrumbs()
                .stream()
                .filter(screen -> screen instanceof Dashboardscreen)
                .forEach(screen -> ((Dashboardscreen) screen).reloadTasks());

Thanks again!