Prevent close does not really prevent close

Code:

@Subscribe
public void onBeforeClose(final BeforeCloseEvent event) {
        if (isNew) {
            event.preventClose();
            dialogs.createOptionDialog()
                    .withHeader("Confirm?")
                    .withText("Data CANNOT be changed after closing. Are you sure you want to leave?")
                    .withActions(
                            new DialogAction(DialogAction.Type.YES)
                                      .withHandler(e -> dataContext.save()),
                            new DialogAction(DialogAction.Type.NO)
                    )
                    .open();
        }
    }

The expected result would be:

  1. User wants to close / open another screen
  2. Dialog pops-up, but the view remains (it will not be closed, and the other view will not be opened)

The actual result:

  1. User clicks on another menu
  2. The menu (new view) opened
  3. The dialog shows up.

Is this a bug or expected? And if it’s expected, is there any workaround?

Hi,

In case of navigation, you need to pospone it too, e.g.:

@Subscribe
public void onBeforeClose(final BeforeCloseEvent event) {
...
    CloseAction action = event.getCloseAction();
    if (action instanceof NavigateCloseAction) {
        BeforeLeaveEvent beforeLeaveEvent = ((NavigateCloseAction) action).getBeforeLeaveEvent();
        beforeLeaveEvent.postpone();
    }

    event.preventClose();
...

The more complex example that considers both dialog mode and navigation can be found in the io.jmix.flowui.view.StandardDetailView#preventUnsavedChanges method that handles default preventUnsavedChanges logic.

Regards,
Gleb