Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ wearToolingPreview = "1.0.0"
webkit = "1.15.0"
wfp = "1.0.0-rc01"
androidx-core-telecom = "1.1.0-alpha04"
wearCompose = "1.6.0"



[libraries]
accompanist-adaptive = "com.google.accompanist:accompanist-adaptive:0.37.3"
Expand Down Expand Up @@ -293,6 +296,9 @@ validator-push = { module = "com.google.android.wearable.watchface.validator:val
wear-compose-material = { module = "androidx.wear.compose:compose-material", version.ref = "wearComposeMaterial" }
wear-compose-material3 = { module = "androidx.wear.compose:compose-material3", version.ref = "wearComposeMaterial3" }
androidx-core-telecom = { group = "androidx.core", name = "core-telecom", version.ref = "androidx-core-telecom" }
wear-compose-navigation3 = { module = "androidx.wear.compose:compose-navigation3", version.ref = "wearCompose" }



[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
Expand Down
7 changes: 7 additions & 0 deletions wear/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.roborazzi)
alias(libs.plugins.kotlin.serialization)
}

android {
Expand Down Expand Up @@ -59,6 +60,12 @@ android {
}

dependencies {
implementation(libs.androidx.navigation3.runtime)
implementation(libs.androidx.navigation3.ui)
implementation(libs.wear.compose.navigation3)
implementation(libs.kotlinx.serialization.json)
implementation(libs.androidx.lifecycle.viewmodel.navigation3)

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.credentials)
implementation((libs.androidx.credentials.play.services.auth))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.wear.snippets.m3.navigation

import androidx.compose.runtime.Composable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.navigation3.rememberViewModelStoreNavEntryDecorator
import androidx.navigation3.runtime.NavKey
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.runtime.rememberNavBackStack
import androidx.navigation3.runtime.rememberSaveableStateHolderNavEntryDecorator
import androidx.navigation3.ui.NavDisplay
import androidx.wear.compose.navigation3.rememberSwipeDismissableSceneStrategy
import kotlinx.serialization.Serializable

// [START android_wear_nav3_destinations]
@Serializable
sealed interface Screen : NavKey {
@Serializable
data object Home : Screen

@Serializable
data class Details(val itemId: String) : Screen
}
// [END android_wear_nav3_destinations]

// [START android_wear_nav3_setup]
@Composable
fun WearApp() {
// 1. Create the persistent back stack starting at the Home screen
val backStack = rememberNavBackStack(Screen.Home)

// 2. Initialize the Wear OS swipe-to-dismiss strategy
val strategy = rememberSwipeDismissableSceneStrategy<NavKey>()
Comment thread
ithinkihaveacat marked this conversation as resolved.

// 3. Render the NavDisplay
NavDisplay(
backStack = backStack,
sceneStrategies = listOf(strategy),
Comment thread
ithinkihaveacat marked this conversation as resolved.
entryProvider = entryProvider {
// 4. Map keys to Composables
entry<Screen.Home> {
HomeScreen(
onNavigateToDetails = { id -> backStack.add(Screen.Details(id)) }
)
}
entry<Screen.Details> { key ->
DetailsScreen(
itemId = key.itemId,
onBack = { backStack.removeLast() }
)
}
}
)
}
// [END android_wear_nav3_setup]

@Composable
fun WearAppWithViewModel() {
val backStack = rememberNavBackStack(Screen.Home)
val strategy = rememberSwipeDismissableSceneStrategy<NavKey>()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The rememberSwipeDismissableSceneStrategy function requires an onDismiss lambda to handle the back navigation when a swipe gesture is completed. Without this, the swipe-to-dismiss gesture will not update the back stack.

Suggested change
val strategy = rememberSwipeDismissableSceneStrategy<NavKey>()
val strategy = rememberSwipeDismissableSceneStrategy<NavKey> {
backStack.removeLast()
}


// [START android_wear_nav3_viewmodel]
NavDisplay(
backStack = backStack,
sceneStrategies = listOf(strategy),
entryDecorators = listOf(
rememberSaveableStateHolderNavEntryDecorator<NavKey>(),
rememberViewModelStoreNavEntryDecorator<NavKey>()
),
entryProvider = entryProvider {
entry<Screen.Home> {
// Any viewModel() requested here will be scoped to this NavEntry
val viewModel: HomeViewModel = viewModel()
HomeScreen(onNavigateToDetails = {})
}
}
)
// [END android_wear_nav3_viewmodel]
}

// Mock implementations to make it compile
@Composable
fun HomeScreen(onNavigateToDetails: (String) -> Unit) {
// Mock
}

@Composable
fun DetailsScreen(itemId: String, onBack: () -> Unit) {
// Mock
}

class HomeViewModel : ViewModel() {
// Mock
}
Loading