Navigation with Compose for Wear OS


The Navigation component in Android Jetpack provides support for Jetpack Compose applications. You can navigate between composables while taking advantage of the Navigation component's infrastructure and features.

This page describes the differences with Jetpack Navigation on Compose for Wear OS.

Setup

Use the following dependency in your app module's build.gradle file:

Kotlin

dependencies {
    def wear_compose_version = "1.7.2"
    implementation "androidx.wear.compose:compose-navigation:$wear_compose_version"
}

This is used instead of the androidx.navigation:navigation-compose artifact because it provides alternative implementations specific to Wear OS.

Create a navigation controller, host and graph

Navigating with Compose for Wear OS requires the same three components needed on non-Wear OS apps: the navigation controller, host and graph.

Use rememberSwipeDismissableNavController() to create an instance of WearNavigator, an implementation of NavController suitable for Wear OS applications:

val navController = rememberSwipeDismissableNavController()

The NavController is the primary API used to navigate in Compose applications. It controls navigating between composables in the navigation host which, on Wear OS, is SwipeDismissableNavHost.

val navController = rememberSwipeDismissableNavController()
SwipeDismissableNavHost(
    navController = navController,
    startDestination = "message_list"
) {
    // TODO: build navigation graph
}

Like the NavHost composable, it takes a reference to the navigation controller, the route for the start destination, and the builder for the navigation graph which is shown here as a trailing lambda.

The start destination must be provided in the navigation graph builder, along with all other destinations that should be navigable with the navigation controller.

In your Wear OS app, declare SwipeDismissableNavHost as a content of the AppScaffold to support top-level components like time, scroll or position indicator, and page indicator. Use a AppScaffold object above the SwipeDismissableNavHost and the ScreenScaffold at the screen level to add a TimeText object to the screen by default and to make sure it animates correctly when navigating between screens. Additionally, ScreenScaffold adds a PositionIndicator for scrollable content.
    AppScaffold {
        val navController = rememberSwipeDismissableNavController()
        SwipeDismissableNavHost(
            navController = navController,
            startDestination = "message_list"
        ) {
            composable("message_list") {
                MessageList(onMessageClick = { id ->
                    navController.navigate("message_detail/$id")
                })
            }
            composable("message_detail/{id}") {
                MessageDetail(id = it.arguments?.getString("id")!!)
            }
        }
    }
}

// Implementation of one of the screens in the navigation
@Composable
fun MessageDetail(id: String) {
    // .. Screen level content goes here
    val scrollState = rememberTransformingLazyColumnState()
    val transformationSpec = rememberTransformationSpec()

    ScreenScaffold(
        scrollState = scrollState,
    ) { contentPadding ->
        // Screen content goes here
        // ...

Display dialogs

To display a dialog using Material 3 for Compose on Wear OS—such as AlertDialog, ConfirmationDialog, OpenOnPhoneDialog, or Dialog—declare the dialog composable directly inside the screen composable destination that opens it. To do so, complete these steps:

  1. Control the dialog's visibility using a boolean state variable, and pass that state to the dialog's visible parameter. Use either rememberSaveable or a state that's hoisted to a ViewModel.
  2. In the onDismissRequest callback, and in any other confirmation or dismissal action callbacks, set the state variable to false. That way, the dialog animates out and returns to the underlying screen when the user swipes to dismiss or chooses an action.

For more information, see the AlertDialog, ConfirmationDialog, and OpenOnPhoneDialog API reference documentation.

To learn more about Jetpack Navigation, see Navigating with Compose or take the Jetpack Compose Navigation code lab.