[英]Navigation Component: Passing bundles to destinations via Bottom Navigation
In my application I have a host fragment for a group of views that a user can navigate to via a BottomNavigationView
. 在我的应用程序中,我有一组用户可以通过BottomNavigationView
导航到的视图的主机片段。 This BottomNavigationView
is connected to my nav controller via setupWithNavController
. 这个BottomNavigationView
通过setupWithNavController
连接到我的导航控制器。
My host fragment receives a bundle with some information that I would like each fragment to receive as it is navigated to (via the bottom nav view) as a bundle. 我的主机片段收到一个包含一些信息的包,我希望每个片段在被导航到(通过底部导航视图)作为一个包时被接收。
My current solution looks like 我目前的解决方案看起来像
mutableListOf<NavDestination>().apply {
addIfNotNull(graph.findNode(R.id.frag1))
addIfNotNull(graph.findNode(R.id.frag2))
addIfNotNull(graph.findNode(R.id.frag3))
forEach {
// args is a safe args object for this host fragment
it.addArgument("argName", NavArgument.Builder().setDefaultValue(args.argName).build())
}
}
While this works it will not scale very well as I am manually adding the arguments for each destination. 虽然这是有效的,但它不能很好地扩展,因为我手动为每个目标添加参数。 Since I am not manually navigating to each destination, rather it is done by the BottomNavigationView
I'm not sure how to manually add this bundle. 由于我没有手动导航到每个目的地,而是由BottomNavigationView
完成,我不知道如何手动添加此捆绑包。
navController.addOnDestinationChangedListener { controller, dest, args ->
when (dest.label) {
"YOUR_LABEL_HERE" -> {
val arg01 = NavArgument.Builder().setDefaultValue("SOME VALUE").build()
val arg02 = NavArgument.Builder().setDefaultValue("SOME OTHER VALUE").build()
dest.addArgument("KEY_NAME", arg01)
dest.addArgument("OTHER_KEY_NAME", arg02)
}
}
}
try this. 尝试这个。 It should work fine. 它应该工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.