AANavigationStateMachine
State machine managing Android Auto screen navigation transitions.
This class provides centralized control over screen navigation, ensuring:
Only valid transitions are allowed
State changes are tracked and observable
Invalid transitions are logged with helpful error messages
Navigation history can be queried
Design Goals
Predictability: All valid transitions are explicitly defined
Debuggability: Invalid transitions are logged with context
Observability: State changes are exposed via StateFlow
Testability: Pure state machine logic, no side effects
Valid Transitions
| From State | Action | To State |
|---|---|---|
| Disconnected | Connect | MapView |
| Any | Disconnect | Disconnected |
| MapView | OpenSearch | Search |
| Search | SelectDestination | RoutePreview |
| Search | Back | MapView |
| RoutePreview | StartNavigation | Navigation |
| RoutePreview | Back | Search |
| RoutePreview | CancelRoute | MapView |
| Navigation | StopNavigation | MapView |
| Navigation | Back | RoutePreview |
| MapView | OpenSettings | Settings |
| Settings | Back | MapView |
Integration with ScreenManager
This state machine is intentionally decoupled from ScreenManager. The caller is responsible for:
Calling transition to request a state change
Checking the result
Performing the actual screen push/pop based on the result
This separation allows:
Unit testing the state machine in isolation
Custom screen management strategies
Validation before expensive operations
Custom Transitions
Clients can extend the state machine with additional transitions for custom screens via the additionalTransitions constructor parameter. Additional transitions are merged with the built-in defaults. If a collision is detected (same from-state and action), the built-in transition takes precedence and a warning is logged.
val customTransitions = mapOf(
Pair(AANavigationState.RoutePreview, AANavigationAction.Custom("OpenPayment"))
to AANavigationState.Custom("Payment"),
Pair(AANavigationState.Custom("Payment"), AANavigationAction.Back)
to AANavigationState.RoutePreview
)
val stateMachine = AANavigationStateMachine(additionalTransitions = customTransitions)Parameters
Additional transitions to merge with the built-in defaults. Defaults to empty (standard navigation flow only).
See also
Constructors
Functions
Checks if a transition is valid without executing it.
Clears the transition history.
Forces the state machine to a specific state.
Gets the transition history for debugging.
Gets the list of valid actions from the current state.
Attempts to transition to a new state based on the given action.