androidx.navigationevent.compose

Objects

LocalNavigationEventDispatcherOwner

The CompositionLocal containing the current NavigationEventDispatcher.

Cmn

Top-level functions summary

Unit
@Composable
NavigationEventDispatcherOwner(
    enabled: () -> Boolean,
    parentNavigationEventDispatcherOwner: NavigationEventDispatcherOwner?,
    content: @Composable () -> Unit
)

Creates a new navigation scope by providing a NavigationEventDispatcher to descendant composables.

Cmn
Unit
@Composable
NavigationEventHandler(
    enabled: () -> Boolean,
    onEvent: suspend (progress: Flow<NavigationEvent>) -> Unit
)

Handles predictive back navigation gestures.

Cmn

Top-level functions

@Composable
fun NavigationEventDispatcherOwner(
    enabled: () -> Boolean = { true },
    parentNavigationEventDispatcherOwner: NavigationEventDispatcherOwner? = LocalNavigationEventDispatcherOwner.current,
    content: @Composable () -> Unit
): Unit

Creates a new navigation scope by providing a NavigationEventDispatcher to descendant composables.

This composable creates a dispatcher that links to any parent dispatcher found in the composition, forming a parent-child relationship. If no parent exists, it automatically becomes a new root dispatcher, this is the top-most parent in a hierarchy. This is useful for isolating navigation handling within specific UI sections, such as a self-contained feature screen or tab.

The dispatcher's lifecycle is automatically managed. It is created only once and automatically disposed of when the composable leaves the composition, preventing memory leaks.

When used to create a root dispatcher, you must use a NavigationEventInputHandler to send it events. Otherwise, the dispatcher will be detached and will not receive events.

Parameters
enabled: () -> Boolean = { true }

A lambda to dynamically control if the dispatcher is active. When false, this dispatcher and any of its children will ignore navigation events. Defaults to true.

parentNavigationEventDispatcherOwner: NavigationEventDispatcherOwner? = LocalNavigationEventDispatcherOwner.current

The parent owner to link to. Defaults to the owner found in the current composition (LocalNavigationEventDispatcherOwner).

content: @Composable () -> Unit

The child composable content that will receive the new dispatcher.

@Composable
fun NavigationEventHandler(
    enabled: () -> Boolean = { true },
    onEvent: suspend (progress: Flow<NavigationEvent>) -> Unit
): Unit

Handles predictive back navigation gestures.

This effect registers a handler with the NavigationEventDispatcher provided by the LocalNavigationEventDispatcherOwner. The handler receives updates on the progress of system back gestures as a Flow of NavigationEvent.

Use it as follows:

NavigationEventHandler { progress: Flow<NavigationEvent> ->
// This block is executed when the back gesture begins.
try {
progress.collect { backEvent ->
// Handle gesture progress updates here.
}
// This block is executed if the gesture completes successfully.
} finally {
// This block is executed if the gesture is cancelled.
}
}

When multiple NavigationEventHandler are present, only the innermost enabled handler will receive the gesture events. If no handlers are enabled, the event will propagate up the hierarchy.

Parameters
enabled: () -> Boolean = { true }

Set to false to disable this handler. Defaults to true.

onEvent: suspend (progress: Flow<NavigationEvent>) -> Unit

The lambda that receives the flow of back gesture events.