Jetpack导航-以编程方式检索嵌套图

时间:2020-04-19 18:19:34

标签: android android-jetpack android-jetpack-navigation

我想以编程方式检索嵌套图以更改startDestination

适用于navHost的

val navHostFragment = appNavFragment as NavHostFragment
val inflater = navHostFragment.navController.navInflater
val graph = inflater.inflate(R.navigation.nav_app)
graph.startDestination = if(sth) startDestination1 else startDestination2

val navController = navHostFragment.navController

navController.graph = graph

我想对嵌套图执行相同的操作,但是我不知道如何以编程方式检索它。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,对于我需要使用findNode

的嵌套图
    val node = graph.findNode(R.id.nav_main)
    (node as NavGraph).startDestination = R.id.onboarding_dest