JetNews: Migrate from Navigation 2 to Navigation 3#1644
JetNews: Migrate from Navigation 2 to Navigation 3#1644stevenelliottjr wants to merge 2 commits intoandroid:mainfrom
Conversation
Replace NavHost/NavController with NavDisplay and a developer-owned SnapshotStateList backstack. String routes are replaced with @serializable NavKey types for type-safe navigation. Fixes android#1632
Summary of ChangesHello @stevenelliottjr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the JetNews application's navigation architecture. The core change involves migrating from the traditional Navigation Compose (Navigation 2) to the newer Navigation 3 library. This transition enhances type safety for navigation routes and provides developers with direct control over the navigation backstack, leading to a more robust and predictable navigation experience. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request successfully migrates the JetNews app from Jetpack Navigation 2 to Navigation 3, adopting type-safe routes with Kotlin Serialization and a developer-owned backstack. The changes are well-implemented and follow the new Navigation 3 paradigm. I've provided a couple of suggestions to improve UI consistency and optimize navigation logic.
| val navigationActions = remember(navController) { | ||
| JetnewsNavigationActions(navController) | ||
| val backStack = remember { mutableStateListOf<JetnewsRoute>(startRoute) } | ||
| val currentRoute: JetnewsRoute = backStack.lastOrNull() ?: Home() |
There was a problem hiding this comment.
When the backStack becomes empty (e.g., when the user presses back on the last screen), currentRoute defaults to Home(). This causes AppDrawer and AppNavRail to show "Home" as selected, while NavDisplay shows a blank screen because its backstack is empty. This creates a UI inconsistency.
To fix this, currentRoute should be nullable. When it's null, no navigation item will be selected, which correctly reflects the empty state.
You'll also need to update the currentRoute parameter in AppDrawer and AppNavRail to be nullable (JetnewsRoute?). The is checks for selection will continue to work correctly.
| val currentRoute: JetnewsRoute = backStack.lastOrNull() ?: Home() | |
| val currentRoute: JetnewsRoute? = backStack.lastOrNull() |
| // Pop to Home, then add Interests on top (single top behavior) | ||
| while (backStack.size > 1) backStack.removeLast() | ||
| backStack.add(Interests) |
There was a problem hiding this comment.
The current implementation for navigateToInterests always pops back to the home route before adding the Interests route. If the user is already on the Interests screen, this causes the screen to be unnecessarily removed and re-added, which can lead to inefficient recomposition and potential UI flicker. This behavior doesn't fully match the launchSingleTop flag from Navigation 2, which would prevent this.
To optimize this and align closer with launchSingleTop, you can add a check to do nothing if Interests is already the current route.
| // Pop to Home, then add Interests on top (single top behavior) | |
| while (backStack.size > 1) backStack.removeLast() | |
| backStack.add(Interests) | |
| if (backStack.lastOrNull() == Interests) return@lambda | |
| while (backStack.size > 1) backStack.removeLast() | |
| backStack.add(Interests) |
Make currentRoute nullable to avoid showing Home as selected when the backstack is empty. Add single-top guard to navigateToInterests to prevent unnecessary remove-and-re-add when already on Interests.
Summary
NavHost/NavControllerwith Nav3'sNavDisplayand a developer-ownedSnapshotStateListbackstackJetnewsDestinations) with@SerializableNavKeytypes (Home,Interests) for type-safe navigationnavDeepLink {}DSL to manual intent parsing inMainActivityChanges
libs.versions.tomlnavigation3-runtime,navigation3-ui,lifecycle-viewmodel-navigation3build.gradle.ktskotlin.serializationpluginJetnewsNavigation.ktJetnewsDestinations/JetnewsNavigationActions, addedsealed interface JetnewsRoute : NavKeyJetnewsNavGraph.ktNavHost→NavDisplaywithentryProviderlambda and Nav3 decoratorsJetnewsApp.ktrememberNavController()→mutableStateListOf<JetnewsRoute>(), navigation via list opsAppDrawer.ktcurrentRoute: String→currentRoute: JetnewsRoute,istype checksAppNavRail.ktMainActivity.ktintent?.data?.getQueryParameter("postId"))Test plan
./gradlew :JetNews:app:assembleDebugbuilds successfullyadb shell am start -a android.intent.action.VIEW -d "https://developer.android.com/jetnews/home?postId=post5"Fixes #1632