自定义“向上导航"使用导航组件的某些片段的行为 [英] Custom "navigate up" behavior for certain fragment using Navigation component
问题描述
我想使用导航组件从片段中添加自定义向上导航
在我的build.gradle(app)
中,我使用androidx.appcompat:appcompat:1.1.0-alpha04
依赖项来从活动中访问onBackPressedDispatcher
.
所以我在片段中实现了OnBackPressedCallback
注册到调度员的回调:
requireActivity().onBackPressedDispatcher.addCallback(this)
我希望在工具栏中按向上导航会调用它,但是不会. 按设备的后退按钮可以正常调用它.
是否有类似的方法可以在向上导航操作中在片段中添加一些回调?
更新
按下工具栏中的向上按钮时,不会调用覆盖的方法onOptionsItemSelected
和onSupportNavigateUp
我找到了解决方法
handleOnBackPressed()
方法仅在设备的后退按钮单击时调用.
我想知道为什么在按下工具栏中的向上按钮"时都没有调用onOptionsItemSelected()
和onSupportNavigateUp()
方法.答案是我用
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration)
活动以设置带有导航组件的工具栏. 这样一来,工具栏就可以响应内部导航,按下向上按钮"并没有在活动或片段中调用任何覆盖的方法.
应该使用
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
这将使actionBar响应导航,因此我可以使用覆盖的功能onOptionsItemSelected()
和onSupportNavigateUp()
对于某些屏幕,在向上按钮"上单击以添加自定义行为的最佳位置(就我而言)是
onSupportNavigateUp()
托管活动之类的
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.mainNavHostFragment)
return when(navController.currentDestination?.id) {
R.id.destinationOfInterest -> {
// custom behavior here
true
}
else -> navController.navigateUp()
}
}
但是值得一提的是,如果您想直接实现片段的自定义行为,@ Enzokie的答案应该像一个魅力
I want to add custom up navigation from fragment using Navigation component
In my build.gradle(app)
I use androidx.appcompat:appcompat:1.1.0-alpha04
dependency to have access to onBackPressedDispatcher
from activity.
So I implemented OnBackPressedCallback
in my fragment and
registered callback to dispatcher:
requireActivity().onBackPressedDispatcher.addCallback(this)
I expected that pressing navigate up in toolbar will call it, but it doesn't. Pressing device's back button calls it as expected.
Is there a similar way to add some callback in fragment on navigate up action?
UPDATE
overridden methods onOptionsItemSelected
and onSupportNavigateUp
doesn't invoked on pressing up button in toolbar
I found a solution
handleOnBackPressed()
method invokes only on device's back button click.
I wonder, why neither onOptionsItemSelected()
nor onSupportNavigateUp()
methods haven't been called on pressing "up button" in toolbar. And the answer is I used
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration)
in activity to setup toolbar with navigation component. And that made toolbar responsive for work with navigation internally, pressing "up button" haven't invoked any of overridden methods in activity or fragments.
Instead should be used
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
That will make actionBar responsive for navigation, thus I can use overridden functions onOptionsItemSelected()
and onSupportNavigateUp()
And best place (in my case) to add custom behavior on "up button" click for certain screen is
onSupportNavigateUp()
of hosted activity, like that
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.mainNavHostFragment)
return when(navController.currentDestination?.id) {
R.id.destinationOfInterest -> {
// custom behavior here
true
}
else -> navController.navigateUp()
}
}
But worth to say, that if you want implement custom behavior directly in fragment, answer of @Enzokie should work like a charm
这篇关于自定义“向上导航"使用导航组件的某些片段的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!