Lifecycle
This article covers the lifetime of screen instances and their integration with the Android Lifecycle.
Screen Instance Lifecycle
Screen instances in Modo have a long lifetime:
Screen instances live as long as your app process (not tied to Activity or Fragment lifecycle)
The same instance survives recomposition and configuration changes (rotation, language change, etc.)
Safe to inject into your DI container if its lifetime is shorter or equal to the screen
This is guaranteed when using Modo.rememberRootScreen() and similar built-in functions.
Lifecycle Basics
Modo provides seamless integration with Android Lifecycle. Each screen gets its own LifecycleOwner that can be retrieved by using LocalLifecycleOwner:
Screen lifecycle is controlled by three main factors:
Composition state: Lifecycle responds to entering/leaving composition
Parent lifecycle: Child state never exceeds parent state and can be updated by a parent (see Parent-Child Lifecycle Coordination)
Transitions: Screen can be resumed only after animations are complete (see Transition Lifecycle Control)
Each screen progresses through the standard Android Lifecycle states:
State | Meaning |
|---|---|
INITIALIZED | The screen is constructed (instance created) but has never been displayed. |
CREATED | The screen was displayed at least once. |
STARTED | The screen is in composition. |
RESUMED | Ready for user interaction. The screen is STARTED, visible, and all transitions are complete. |
DESTROYED | The screen is removed from the navigation graph, and all resources are cleaned up. |
Screens move through states sequentially. Each transition happens when something changes in the screen's lifecycle:
Parent-Child Lifecycle Coordination
The lifecycle of parent and child screens follows strict rules to ensure consistency:
Rules
Rule: A child's lifecycle state never exceeds its parent's state.
Event Propagation from Parent:
Parent Event | Propagation Behavior |
|---|---|
| Never propagated (child subscribes after its own creation) |
| Always propagated → Child moves to STARTED |
| Propagated but gated by transitions and activation → Child moves to RESUMED only if all conditions are met |
| Always propagated → Child immediately moves to STARTED |
| Always propagated → Child immediately moves to CREATED |
| Conditionally propagated (blocked during config changes to preserve SavedStateRegistry) |
Examples
Parent with transition, child without transition
Even though the child has no animation, it waits at STARTED until the parent reaches RESUMED.
Screen Rotation (Configuration Change)
During configuration changes, screens are preserved (not destroyed) and reattach to the new Activity instance.
Transition Lifecycle Control
Screens animated by ScreenTransition cannot reach RESUMED state until the animation completes:
Without transition: STARTED → RESUMED (immediate)
With transition: STARTED → (animation playing) → RESUMED (after animation completes)
This indirectly affects nested screens because of parent-child lifecycle propagation.
Practical Examples
Managing Keyboard
Show/hide keyboard based on screen visibility:
Debugging Screens Lifecycle
Enable logging to see lifecycle events: