[英]How to display back button when navigating to self with navigation component?
I have set up my navigation bar like this我已经像这样设置了我的导航栏
with(ActivityHolderBinding.inflate(layoutInflater)) {
setContentView(root)
val fragment = supportFragmentManager.findFragmentByTag("holder") as NavHostFragment
toolbar.setupWithNavController(fragment.navController,AppBarConfiguration(fragment.navController.graph))
setSupportActionBar(toolbar)
}
But this only shows a back button when I navigate to another fragment and not when I navigate to self on the fragment marked as start destination.但这仅在我导航到另一个片段时显示后退按钮,而不是在标记为开始目的地的片段上导航到 self 时。
As per this issue :根据这个问题:
setupActionBarWithNavController uses the current destination ID to determine if the Up button is shown, so the behavior you're seeing is working as intended.
setupActionBarWithNavController 使用当前目标 ID 来确定是否显示向上按钮,因此您看到的行为按预期工作。
You can have multiple destinations that use the same Fragment class, so just create a separate destination for recursive calls:
您可以有多个使用相同 Fragment 类的目的地,因此只需为递归调用创建一个单独的目的地:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/catalog_nav_graph"
app:startDestination="@id/categories">
<fragment
android:id="@+id/categories"
android:name="somepackage.categories.CategoryListFragment"
tools:layout="@layout/catalog_category_list_frag">
<action
android:id="@+id/action_category_to_category"
app:destination="@id/categoriesRecursive" />
<action
android:id="@+id/action_category_to_product_list"
app:destination="@id/products_frag" />
<argument
android:name="categoryId"
app:argType="integer"
android:defaultValue="0" />
</fragment>
<fragment
android:id="@+id/categoriesRecursive"
android:name="somepackage.categories.CategoryListFragment"
tools:layout="@layout/catalog_category_list_frag">
<action
android:id="@+id/action_category_to_category"
app:destination="@id/categoriesRecursive" />
<action
android:id="@+id/action_category_to_product_list"
app:destination="@id/products_frag" />
<argument
android:name="categoryId"
app:argType="integer"
android:defaultValue="0" />
</fragment>
....
</navigation>
So by having two different IDs, it is possible for NavigationUI
to distinguish between your first level (which should not display an up arrow) vs ones that should display an up arrow.因此,通过拥有两个不同的 ID,
NavigationUI
可以区分您的第一级(不应显示向上箭头)与应显示向上箭头的第一级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.