我写了两个扩展来完成这项工作。首先是用于更改当前节点中的目的地。第二种是用来多次遍历成嵌套图
_main_graph
|
|_nested_graph_1
|
|_nested_graph_2
|
|_nested_graph_3
|
|_nested_graph_4
|
|_change this destination!!!!
fun NavController.changeNodeDestination(nodeId: Int, destinationId: Int): NavController {
val graph = graph.findNode(nodeId) as NavGraph
graph.startDestination = destinationId
return this
}
fun NavController.changeNodeDestination(vararg nodeIds: Int, destinationId: Int): NavController {
var currentNode = graph
nodeIds.forEachIndexed { index, i ->
currentNode = currentNode.findNode(nodeIds[index]) as NavGraph
}
currentNode.startDestination = destinationId
return this
}
用法:
findNavController().changeNodeDestination(
R.id.navigation_login,
R.id.nav_change_password, // GO two levels inside nested graphs
destinationId = startDestination
).navigate(navID, bundle)