-
Notifications
You must be signed in to change notification settings - Fork 365
Add Wear navigation 3 snippets #922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ithinkihaveacat
merged 5 commits into
android:main
from
ithinkihaveacat:migrate-nav3-snippets
May 18, 2026
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3c40631
Migrate Navigation 3 snippets
ithinkihaveacat a1cd100
Use version catalog for wear-compose-navigation3
ithinkihaveacat 7fb162a
Fix NewApi lint error in Navigation.kt
ithinkihaveacat 9773ffa
Add Navigation 3 migration snippets to wear module
ithinkihaveacat c4fc5fb
Merge branch 'main' into migrate-nav3-snippets
ithinkihaveacat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
wear/src/main/java/com/example/wear/snippets/navigation3/Navigation.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * 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.navigation3 | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.lifecycle.viewmodel.compose.viewModel | ||
| 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.material3.ScreenScaffold | ||
| import androidx.wear.compose.material3.Text | ||
| import androidx.wear.compose.navigation3.rememberSwipeDismissableSceneStrategy | ||
| import kotlinx.serialization.Serializable | ||
| import androidx.lifecycle.viewmodel.navigation3.rememberViewModelStoreNavEntryDecorator | ||
|
|
||
| // [START android_wear_navigation3_destinations] | ||
| @Serializable | ||
| sealed interface Screen : NavKey { | ||
| @Serializable | ||
| data object Home : Screen | ||
|
|
||
| @Serializable | ||
| data class Details(val itemId: String) : Screen | ||
| } | ||
| // [END android_wear_navigation3_destinations] | ||
|
|
||
| @Composable | ||
| fun WearApp() { | ||
| // [START android_wear_navigation3_setup] | ||
| // 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>() | ||
|
|
||
| // 3. Render the NavDisplay | ||
| NavDisplay( | ||
| backStack = backStack, | ||
| sceneStrategies = listOf(strategy), | ||
| 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_navigation3_setup] | ||
| } | ||
|
|
||
| // [START_EXCLUDE] | ||
| @Composable | ||
| fun HomeScreen(onNavigateToDetails: (String) -> Unit) { | ||
| ScreenScaffold { | ||
| Text("Home Screen") | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| fun DetailsScreen(itemId: String, onBack: () -> Unit) { | ||
| ScreenScaffold { | ||
| Text("Details Screen for item $itemId") | ||
| } | ||
| } | ||
|
|
||
| class HomeViewModel : androidx.lifecycle.ViewModel() | ||
| // [END_EXCLUDE] | ||
|
|
||
| @Composable | ||
| fun WearAppWithViewModel() { | ||
| val backStack = rememberNavBackStack(Screen.Home) | ||
| val strategy = rememberSwipeDismissableSceneStrategy<NavKey>() | ||
|
|
||
| // [START android_wear_navigation3_viewmodel] | ||
| NavDisplay( | ||
| backStack = backStack, | ||
| sceneStrategies = listOf(strategy), | ||
| entryDecorators = listOf( | ||
| rememberSaveableStateHolderNavEntryDecorator(), | ||
| rememberViewModelStoreNavEntryDecorator() | ||
| ), | ||
| entryProvider = entryProvider { | ||
| entry<Screen.Home> { | ||
| // Any viewModel() requested here will be scoped to this NavEntry | ||
| val viewModel: HomeViewModel = viewModel() | ||
| // [START_EXCLUDE silent] | ||
| HomeScreen(onNavigateToDetails = {}) | ||
| // [END_EXCLUDE] | ||
| } | ||
| } | ||
| ) | ||
| // [END android_wear_navigation3_viewmodel] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.