NavigationEventHandler

Functions summary

Unit
@Composable
NavigationEventHandler(
    state: NavigationEventState<NavigationEventInfo>,
    isForwardEnabled: Boolean,
    onForwardCancelled: () -> Unit,
    onForwardCompleted: () -> Unit,
    isBackEnabled: Boolean,
    onBackCancelled: () -> Unit,
    onBackCompleted: () -> Unit
)

A composable that handles navigation events using simple lambda handlers, driven by a manually hoisted NavigationEventState.

Cmn

Functions

@Composable
fun NavigationEventHandler(
    state: NavigationEventState<NavigationEventInfo>,
    isForwardEnabled: Boolean = true,
    onForwardCancelled: () -> Unit = {},
    onForwardCompleted: () -> Unit = {},
    isBackEnabled: Boolean = true,
    onBackCancelled: () -> Unit = {},
    onBackCompleted: () -> Unit = {}
): Unit

A composable that handles navigation events using simple lambda handlers, driven by a manually hoisted NavigationEventState.

This is the core implementation of the navigation event handler. This overload must be used when you need to hoist the NavigationEventState (by calling rememberNavigationEventState at a higher level). Hoisting is necessary when other composables need to react to the gesture's NavigationEventTransitionState (held within the state object), for example, to drive custom animations.

Precedence

When multiple NavigationEventHandler are present in the composition, the one that is composed last among all enabled handlers will be invoked.

Usage

It is important to call this composable unconditionally. Use isBackEnabled and isForwardEnabled to control whether the handler is active. This is preferable to conditionally calling NavigationEventHandler (e.g., inside an if block), as conditional calls can change the order of composition, leading to unpredictable behavior where different handlers are invoked after recomposition.

Timing Consideration

There are cases where a predictive back or forward gesture may be dispatched within a rendering frame before the corresponding enabled flag is updated, which can cause unexpected behavior (see b/375343407, b/384186542). For example, if isBackEnabled is set to false, a back gesture initiated in the same frame may still trigger this handler because the system sees the stale true value.

Parameters
state: NavigationEventState<NavigationEventInfo>

The hoisted NavigationEventState (returned from rememberNavigationEventState) to be registered. This object links this handler's callbacks to the unique handler instance that is producing the state.

isForwardEnabled: Boolean = true

Controls whether forward navigation gestures are handled.

onForwardCancelled: () -> Unit = {}

Called if a forward navigation gesture is cancelled.

onForwardCompleted: () -> Unit = {}

Called when a forward navigation gesture completes.

isBackEnabled: Boolean = true

Controls whether back navigation gestures are handled.

onBackCancelled: () -> Unit = {}

Called if a back navigation gesture is cancelled.

onBackCompleted: () -> Unit = {}

Called when a back navigation gesture completes.

Throws
IllegalArgumentException

If the provided NavigationEventState is passed to multiple NavigationEventHandler Composable. Each handler must have its own unique state.