ScreenFragment Events

This section describes the lifecycle events that can be handled in fragment controllers.

InitEvent

This event is sent when the fragment controller and all its declaratively defined components are created, and dependency injection is completed. Nested fragments are not initialized yet. Some visual components are not fully initialized, for example, buttons are not linked with actions. If the fragment is attached to the host screen declaratively in XML, this event is sent after InitEvent of the host controller. Otherwise, it is sent when the fragment is added to the host’s component tree.

AfterInitEvent

This event is sent when the fragment controller and all its declaratively defined components are created, dependency injection is completed, and all components have completed their internal initialization procedures. Nested screen fragments (if any) have sent their InitEvent and AfterInitEvent. In this event listener, you can create visual and data components and perform additional initialization if it depends on initialized nested fragments.

AttachEvent

This event is sent when the fragment is added to the host’s component tree. At this moment, the fragment is fully initialized, InitEvent and AfterInitEvent have been sent. In this event listener, you can access the host screen using getHostScreen() and getHostController() methods.

An example of listening to AttachEvent:

@UiController("sample_AddressFragment")
@UiDescriptor("address-fragment.xml")
public class AddressFragment extends ScreenFragment {
    private static final Logger log = LoggerFactory.getLogger(AddressFragment.class);

    @Subscribe
    private void onAttach(AttachEvent event) {
        Screen hostScreen = getHostScreen();
        FrameOwner hostController = getHostController();
        log.info("onAttach to screen {} with controller {}", hostScreen, hostController);
    }

DetachEvent

This event` is sent when the fragment is programmatically removed from the host’s component tree. You cannot access the host screen in this event listener.

Listening to Host Screen Events

In a fragment controller, you can also subscribe to events of the host screen by specifying the PARENT_CONTROLLER value in the target attribute of the annotation, for example:

@Subscribe(target = Target.PARENT_CONTROLLER)
private void onBeforeShowHost(Screen.BeforeShowEvent event) {
    // ...
}

Any event can be handled this way including InitEntityEvent sent by entity editors.