3.5.1.1.3. ScreenFragment Events
This section describes the lifecycle events that can be handled in fragment controllers.
-
InitEvent
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
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 theirInitEvent
andAfterInitEvent
. In this event listener, you can create visual and data components and perform additional initialization if it depends on initialized nested fragments.
-
AttachEvent
is sent when the fragment is added to the host’s component tree. At this moment, the fragment is fully initialized,InitEvent
andAfterInitEvent
have been sent. In this event listener, you can access the host screen usinggetHostScreen()
andgetHostController()
methods.
-
DetachEvent
is sent when the fragment is programmatically removed from the host’s component tree. You cannot access the host screen in this event listener.
An example of listening to fragment events:
@UiController("demo_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);
}
@Subscribe
private void onDetach(DetachEvent event) {
log.info("onDetach");
}
}
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.