活动中有这样一种方法,负责通过URI打开碎片
...
fun navigate(uri: Uri, savedInstanceState: Bundle?) {
with(supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment) {
val authGraph: NavGraph = navController.navInflater.inflate(R.navigation.auth_nav_graph)
maybeSetIapNavGraphs(navHostFragment.navController, authGraph)
navController.graph = authGraph
if (savedInstanceState == null) {
navController.popBackStack()
navController.navigate(uri)
}
}
}
...
我需要向将要打开的片段传递一个布尔值,但是,我看到navigate
没有将Bundle
作为参数,并且唯一可用的Uri
构造函数是.navigate(uri, NavOptions)
,所以看起来没有办法传递一个bundle。
问题是-如何传递一个布尔值到片段?
UPD
我也试着这样做:
...
fun navigate(uri: Uri, savedInstanceState: Bundle?) {
with(supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment) {
val authGraph: NavGraph = navController.navInflater.inflate(R.navigation.auth_nav_graph)
maybeSetIapNavGraphs(navHostFragment.navController, authGraph)
val bundle = Bundle().apply { putInt("isEnabled", 1) }
navController.setGraph(authGraph, bundle)
if (savedInstanceState == null) {
navController.popBackStack()
navController.navigate(uri)
}
}
}
...
但是,当我尝试在片段中获取这个值时,如
val arg1Value = requireArguments().getInt("isEnabled")
它给出0
1条答案
按热度按时间jhkqcmku1#
是的,有可能。在xml配置中定义对额外参数的支持:
之后,应该可以在Bundle over nav控制器内传递数据。更多参考检查this和this。