简体   繁体   English

使用导航组件导航到自己时如何显示后退按钮?

[英]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.

相关问题 导航返回时的 RecyclerView(导航组件) - RecyclerView when navigating back (Navigation Component) 使用导航组件向后导航 - Navigating back with the Navigation Component 为什么我的 android 应用程序在使用后退按钮导航和从 Internet 加载数据时会跳帧? (导航组件) - Why my android app is skipping frames when navigating with back button and loading data fron internet? (Navigation component) 当用户在导航组件中按下返回按钮时如何退出应用程序? - How to exit the app when the user press back button in navigation component? 使用导航组件时如何覆盖工具栏中后退按钮的行为 - How to override the behavior of back button in toolbar when using navigation component 使用导航组件时按下后退按钮退出应用程序而不是导航到上一个屏幕 - Pressing back button exits the app instead of navigating to the previous screen while using navigation component (导航组件)返回首页片段时如何在活动上显示返回箭头? - (Navigation component) How to display back arrow on the activity when back to the home fragment? 使用导航组件和向上导航时如何返回上一个活动(工具栏上的后退按钮) - How to return to previous activity when using Navigation Component and Up navigation (back button on toolbar) 从 D - 导航组件返回时防止破坏(或恢复状态)片段 B - Prevent destroying (or restore state) of Fragment B when navigating back from D - Navigation Component 按钮返回带有导航组件的 startDestination - button back in startDestination with navigation component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM