我正在使用带有bottomNavigationView
. 我已经在我的 AppBar 中配置了MainActivity
一个向上导航按钮,当应用程序不在图表的起始位置时会出现一个向上导航按钮。
我已经像这样设置SupportedActionBar
和覆盖onSupportNavigateUp
:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.mainToolbar)
...
val controller = binding.bottomNavView.setupWithNavController(
navGraphIds = navGraphIds,
fragmentManager = supportFragmentManager,
containerId = R.id.nav_host_container,
intent = intent
)
controller.observe(this, Observer { navController ->
appBarConfiguration = AppBarConfiguration(
topLevelDestinationIds = setOf(
navController.graph.startDestination,
R.id.navigation_level_up_onboarding
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
})
currentController = controller
}
override fun onSupportNavigateUp(): Boolean {
return currentController?.value?.navigateUp() ?: false
}
在导航图中,我有一个ProfileSettingsFragment
需要菜单选项的目的地。我已经将它们设置为:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setHasOptionsMenu(true)
....
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.profile_settings_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}
菜单选项工作正常,但是,一旦我调用 navigateUp 按钮,该片段就会停止工作setHasOptionsMenu(true)
。
我究竟做错了什么?在使用导航组件时,在保持 navigateUp 行为的同时在片段上添加菜单选项的正确方法是什么?