android 将参数传递给Jetpack合成中的嵌套导航图

mo49yndu  于 2023-02-14  发布在  Android
关注(0)|答案(2)|浏览(122)

从文档中,我看到您可以像这样嵌套导航图:

NavHost(navController, startDestination = "home") {
    ...
    // Navigating to the graph via its route ('login') automatically
    // navigates to the graph's start destination - 'username'
    // therefore encapsulating the graph's internal routing logic
    navigation(startDestination = "username", route = "login") {
        composable("username") { ... }
        composable("password") { ... }
        composable("registration") { ... }
    }
    ...
}

我想知道,如何在路由中传递一个参数,并使其对导航图中的所有可组合对象可用?
这是我目前的导航图:

navigation(
    // I'd like to grab this parameter
    route = "dashboard?classId={classId}",
    startDestination = Route.ScreenOne.route) {
    composable(Route.ScreenOne.route) {
        // And then pass the parameter here, or to any composable below
        ScreenOne(classId)
    }
    composable(Route.ScreenTwo.route) {
        ScreenTwo()
    }
    composable(Route.ScreenThree.route) {
        ScreenThree()
    }
}

我基本上是在尝试避免在每个可组合路径上单独设置classId导航参数,我没有找到像composable()那样将参数列表传递给navigation()的方法。
也许我所描述的是不可能的,但期待任何人的想法!

nmpmafwu

nmpmafwu1#

可以从子可合成对象访问图形参数:

navController.getBackStackEntry("dashboard?classId={classId}").arguments?.getString("classId")
zengzsys

zengzsys2#

从基于Hilt的项目中的一个快速测试来看,似乎将属性作为参数传递给navigation图形组件会导致该属性在savedStateHandle中可用于在该图形位于内存中时创建的任何ViewModel
例如:

// 1. Define your routes.
sealed class Destination(val route: String) {
    object TestGraph : Destination("TEST_GRAPH/{${Arguments.testParameter}}") {
        object Arguments {
            const val testParameter = "testParameter"
        }
        fun route(testParameter: String): String {
            return "TEST_GRAPH/$testParameter"
        }
        object FirstScreen : Destination("FIRST_SCREEN")
    }
}
// 2. Create a graph extension on NavGraphBuilder for the navigation graph.
private fun NavGraphBuilder.testGraph(navController: NavHostController) {
    navigation(
        startDestination = Destination.TestGraph.FirstScreen.route,
        route = Destination.TestGraph.route,
        arguments = listOf(
            navArgument(Destination.TestGraph.Arguments.testParameter) { type = NavType.StringType }
        )
    ) {
        composable(route = Destination.TestGraph.FirstScreen.route) {
            FirstScreen()
        }
    }
}
// 3. Use the line below to navigate to this new graph.
navController.navigateTo(Destination.TestGraph.route("xyz"))
// 4. Access the savedStateHandle via the VM to get the parameter.
@Composable
fun FirstScreen(
    viewModel: FirstScreenViewModel = hiltViewModel(),
) {
    //...
}

@HiltViewModel
class FirstScreenViewModel @Inject constructor(
    savedStateHandle: SavedStateHandle,
): ViewModel() {

    private val testParameter: String = checkNotNull(savedStateHandle[Destination.TestGraph.Arguments.testParameter])
    //...
}

我假设这是在导航图级别创建和维护参数时的工作方式。因此,如果图在内存中,则可以通过savedStateHandle访问该属性。如果您要将此图从导航堆栈弹出,我希望该值不再是可访问的。希望这会有所帮助!

相关问题