-
Notifications
You must be signed in to change notification settings - Fork 39
[WIP] Add recipe to show Nav2 to Nav3 migration plan #46
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2025 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 | ||
* | ||
* http://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.nav3recipes.migration.start | ||
|
||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.ui.Modifier | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.rememberNavController | ||
import androidx.navigation.toRoute | ||
import com.example.nav3recipes.ui.setEdgeToEdgeConfig | ||
import kotlinx.serialization.Serializable | ||
|
||
/** | ||
* Basic Navigation2 example with two screens. This will be the starting point for migration to | ||
* Navigation 3. | ||
*/ | ||
|
||
@Serializable | ||
private data object RouteA | ||
|
||
@Serializable | ||
private data class RouteB(val id: String) | ||
|
||
class MainActivity : ComponentActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
setEdgeToEdgeConfig() | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
val navController = rememberNavController() | ||
Scaffold { paddingValues -> | ||
NavHost(navController = navController, startDestination = RouteA, modifier = Modifier.padding(paddingValues)) { | ||
composable<RouteA> { | ||
Column { | ||
Text("Route A") | ||
Button(onClick = { navController.navigate(route = RouteB(id = "123")) }) { | ||
Text("Go to B") | ||
} | ||
} | ||
} | ||
composable<RouteB> { key -> | ||
Text("Route B: ${key.toRoute<RouteB>().id}") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,125 @@ | ||||||||||||||||
/* | ||||||||||||||||
* Copyright 2025 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 | ||||||||||||||||
* | ||||||||||||||||
* http://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.nav3recipes.migration.step1 | ||||||||||||||||
|
||||||||||||||||
import android.os.Bundle | ||||||||||||||||
import androidx.activity.ComponentActivity | ||||||||||||||||
import androidx.activity.compose.setContent | ||||||||||||||||
import androidx.compose.foundation.layout.Column | ||||||||||||||||
import androidx.compose.foundation.layout.padding | ||||||||||||||||
import androidx.compose.material3.Button | ||||||||||||||||
import androidx.compose.material3.Scaffold | ||||||||||||||||
import androidx.compose.material3.Text | ||||||||||||||||
import androidx.compose.runtime.mutableStateListOf | ||||||||||||||||
import androidx.compose.runtime.remember | ||||||||||||||||
import androidx.compose.ui.Modifier | ||||||||||||||||
import androidx.navigation.NavHostController | ||||||||||||||||
import androidx.navigation.compose.NavHost | ||||||||||||||||
import androidx.navigation.compose.composable | ||||||||||||||||
import androidx.navigation.compose.rememberNavController | ||||||||||||||||
import androidx.navigation.toRoute | ||||||||||||||||
import androidx.navigation3.runtime.NavEntry | ||||||||||||||||
import androidx.navigation3.runtime.entryProvider | ||||||||||||||||
import androidx.navigation3.ui.NavDisplay | ||||||||||||||||
import com.example.nav3recipes.ui.setEdgeToEdgeConfig | ||||||||||||||||
import kotlinx.coroutines.CoroutineScope | ||||||||||||||||
import kotlinx.coroutines.Job | ||||||||||||||||
import kotlinx.coroutines.launch | ||||||||||||||||
import kotlinx.serialization.Serializable | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* Basic Navigation2 example with two screens. This will be the starting point for migration to | ||||||||||||||||
* Navigation 3. | ||||||||||||||||
*/ | ||||||||||||||||
|
||||||||||||||||
@Serializable | ||||||||||||||||
private data object RouteA | ||||||||||||||||
|
||||||||||||||||
@Serializable | ||||||||||||||||
private data class RouteB(val id: String) | ||||||||||||||||
|
||||||||||||||||
class MainActivity : ComponentActivity() { | ||||||||||||||||
|
||||||||||||||||
override fun onCreate(savedInstanceState: Bundle?) { | ||||||||||||||||
setEdgeToEdgeConfig() | ||||||||||||||||
super.onCreate(savedInstanceState) | ||||||||||||||||
setContent { | ||||||||||||||||
val navController = rememberNavController() | ||||||||||||||||
val nav3Navigator = remember { Nav3NavigatorSimple(navController) } | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||
|
||||||||||||||||
Scaffold { paddingValues -> | ||||||||||||||||
NavDisplay( | ||||||||||||||||
backStack = nav3Navigator.backStack, | ||||||||||||||||
entryProvider = entryProvider(fallback = { key -> | ||||||||||||||||
println("Key $key not handled by entryProvider, using fallback") | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||
NavEntry(key = key) { | ||||||||||||||||
NavHost( | ||||||||||||||||
navController = navController, | ||||||||||||||||
startDestination = RouteA, | ||||||||||||||||
modifier = Modifier.padding(paddingValues) | ||||||||||||||||
) { | ||||||||||||||||
composable<RouteA> { | ||||||||||||||||
Column { | ||||||||||||||||
Text("Route A") | ||||||||||||||||
Button(onClick = { navController.navigate(route = RouteB(id = "123")) }) { | ||||||||||||||||
Text("Go to B") | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
composable<RouteB> { key -> | ||||||||||||||||
Text("Route B: ${key.toRoute<RouteB>().id}") | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
) { | ||||||||||||||||
// Empty entryProvider | ||||||||||||||||
}) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
class Nav3NavigatorSimple(val navController: NavHostController){ | ||||||||||||||||
|
||||||||||||||||
val coroutineScope = CoroutineScope(Job()) | ||||||||||||||||
Comment on lines
+97
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||
|
||||||||||||||||
// We need a single element to avoid "backStack cannot be empty" error b/430023647 | ||||||||||||||||
val backStack = mutableStateListOf<Any>(Unit) | ||||||||||||||||
|
||||||||||||||||
init { | ||||||||||||||||
coroutineScope.launch { | ||||||||||||||||
navController.currentBackStack.collect { nav2BackStack -> | ||||||||||||||||
with(backStack) { | ||||||||||||||||
if (nav2BackStack.isNotEmpty()){ | ||||||||||||||||
clear() | ||||||||||||||||
val entriesToAdd = nav2BackStack.mapNotNull { entry -> | ||||||||||||||||
// Ignore nav graph root entries | ||||||||||||||||
if (entry.destination::class.qualifiedName == "androidx.navigation.compose.ComposeNavGraphNavigator.ComposeNavGraph"){ | ||||||||||||||||
null | ||||||||||||||||
} else { | ||||||||||||||||
entry | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+112
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking the qualified name of the destination class is brittle and can break with library updates. A more robust way to identify and ignore the root navigation graph entry is to check if its if (entry.destination.parent == null) {
// Ignore nav graph root entries
null
} else {
entry
} |
||||||||||||||||
} | ||||||||||||||||
addAll(entriesToAdd) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure proper lifecycle management and prevent memory leaks, a
CoroutineScope
should be created usingrememberCoroutineScope()
and passed toNav3NavigatorSimple
. This scope is lifecycle-aware and will be cancelled when the composable leaves the composition. This declaration should precede thenav3Navigator
initialization. Don't forget to addimport androidx.compose.runtime.rememberCoroutineScope
.