如果我点击导航控制器中的菜单,如何在两个片段目的地之间传递数据? [英] how to pass data between two fragments destination If I tap a menu in navigation controller?
问题描述
我正在尝试使用导航控制器.我有一个底部导航视图.位于我的 MainActivity 上,它是使用以下代码启动的:
I am trying to use navigation controller. I have a bottom navigation view. that located on my MainActivity, and it is initiated using the code below on :
class MainActivity : AppCompatActivity() {
lateinit var navController : NavController
lateinit var logoHeaderImageView : ImageView
var toolbarMenu : Menu? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
logoHeaderImageView = findViewById(R.id.header_lakuin_image_view)
navController = Navigation.findNavController(this,R.id.nav_main_host_fragment)
setupBottomNavMenu(navController)
setupActionBar(navController)
}
fun setupBottomNavMenu(navController: NavController) {
NavigationUI.setupWithNavController(bottom_navigation_view,navController)
}
fun setupActionBar(navController: NavController) {
setSupportActionBar(main_activity_toolbar)
main_activity_toolbar.title = ""
val appBarConfiguration = AppBarConfiguration(
setOf(
// set some destination as the top hierarchy destination, to make the up button doesn't show.
R.id.destination_home,
R.id.destination_order,
R.id.destination_favourite,
R.id.destination_cart,
R.id.destination_profile
))
NavigationUI.setupWithNavController(main_activity_toolbar,navController,appBarConfiguration)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_main_toolbar, menu)
toolbarMenu = menu
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
}
}
这是我的底部导航视图的外观:
here is the look of my bottom navigation view:
所以我想将数据从我的 HomeFragment
(目的地主页)传递到 OderFragment
(目的地订单).我通常使用 bundle 或 safeArgs 来传递数据,如下面的代码:
So I want to pass data from my HomeFragment
(destination home) to OderFragment
(destination order). I usually using bundle or safeArgs to pass data like the code below:
var bundle = bundleOf("amount" to amount)
view.findNavController().navigate(R.id.confirmationAction, bundle)
但我不知道该代码放在哪里,如果我想将数据从我的 HomeFragment
传递到 OderFragment
but I don't know where to place that code, If I want to pass data from my HomeFragment
to OderFragment
推荐答案
当使用 NavigationUI.setupWithNavController(bottom_navigation_view,navController)
(或者,如果你使用 navigation-ui-ktx
Kotlin 扩展 bottom_navigation_view.setupWithNavController(navController)
),你不能向目的地传递任何自定义参数——全局导航的一个重要部分是它们总是带你到同一个屏幕相同的状态.
When using NavigationUI.setupWithNavController(bottom_navigation_view,navController)
(or, if you're using the navigation-ui-ktx
Kotlin extension bottom_navigation_view.setupWithNavController(navController)
), you can't pass any custom arguments to destinations - an important part of global navigation is that they always take you to the same screen in the same state.
通常,您应该将诸如当前数量之类的数据与导航参数分开保存 - 无论是在持久化数据库、SharedPreferences 中,还是在其他一些可以在进程死亡后幸存的位置中,允许用户继续他们正在做的事情,甚至重启手机等之后
Generally, you should be holding data like the current amount separately from Navigation arguments - whether it is in a persisted database, SharedPreferences, or some other location that would survive process death, allowing users to continue with what they're doing even after restarting their phone, etc.
但是,如果您必须为此使用导航参数,您可以提前为目的地设置默认参数(即,每当您的金额发生变化时):
However, if you must use Navigation arguments for this, you can set the default argument for your destination ahead of time (i.e., whenever your amount changes):
NavDestination orderDestination = navController.graph.findNode(R.id.destination_order)
orderDestination.addArgument("amount", NavArgument.Builder()
.setType(NavType.FloatType)
.setDefaultValue(amount)
.build())
之后,默认情况下,触发 R.id.destination_order
的 BottomNavigationView
将自动包含该参数以及新的 amount
值.
Afterwards, your BottomNavigationView
triggering R.id.destination_order
will automatically include that argument, along with your new amount
value, by default.
这篇关于如果我点击导航控制器中的菜单,如何在两个片段目的地之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!