使用导航组件时,按下后退按钮退出应用程序而不是导航到上一个屏幕

Pressing back button exits the app instead of navigating to the previous screen while using navigation component

我在我的应用程序中使用导航架构组件,每当我导航到一个屏幕然后尝试使用后退按钮 return 到上一个屏幕时,而不是导航回应用程序退出。

我的导航图看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<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/main_nav_host_fragment"
    app:startDestination="@id/authFragment">

    <fragment
        tools:layout="@layout/fragment_main_screen"
        android:id="@+id/sellerListScreenFragment"
        android:name="com.example.paylater.ui.SellerMainScreenFragment"
        android:label="SellerListScreenFragment" >
        <action
            android:id="@+id/action_sellerListScreenFragment_to_creditFragment"
            app:destination="@id/credit_nav_graph" />
    </fragment>
    <fragment
        android:id="@+id/authFragment"
        android:name="com.example.paylater.ui.AuthFragment"
        android:label="AuthFragment"
        tools:layout="@layout/fragment_auth" >
        <action
            android:id="@+id/action_authFragment_to_sellerListScreenFragment"
            app:destination="@id/sellerListScreenFragment" />
    </fragment>

    <navigation android:id="@+id/credit_nav_graph"
        app:startDestination="@id/creditFragment">
        <fragment
            android:id="@+id/creditAddFragment"
            android:name="com.example.paylater.ui.CreditAddFragment"
            android:label="CreditAddFragment"
            tools:layout="@layout/fragment_credit_add">
            <action
                android:id="@+id/action_creditAddFragment_self"
                app:destination="@id/creditAddFragment" />
            <action
                android:id="@+id/action_creditAddFragment_to_transactionResultFragment"
                app:destination="@id/transactionResultFragment" />
        </fragment>
        <fragment
            android:id="@+id/transactionResultFragment"
            android:name="com.example.paylater.ui.TransactionResultFragment"
            android:label="TransactionResultFragment"
            tools:layout="@layout/fragment_transaction_result">
            <action
                android:id="@+id/action_transactionResultFragment_self"
                app:destination="@id/transactionResultFragment" />
        </fragment>
        <fragment
            android:id="@+id/creditFragment"
            android:name="com.example.paylater.ui.CreditFragment"
            android:label="CreditFragment"
            tools:layout="@layout/fragment_credit_enter_no">
            <action
                android:id="@+id/action_creditFragment_to_creditAddFragment"
                app:destination="@id/creditAddFragment" />
            <action
                android:id="@+id/action_creditFragment_self"
                app:destination="@id/creditFragment" />
        </fragment>
    </navigation>
    <action android:id="@+id/action_global_sellerListScreenFragment" app:destination="@id/sellerListScreenFragment"/>
</navigation>

我尝试使用自定义 onBackPressedDispatcher 回调来解决这个问题,但除了导致一些奇怪的行为之外,对每个片段都这样做也很乏味,而且似乎对某些事情来说工作量太大应该由导航组件处理。

有没有办法解决这个问题,因为我已经检查了我的代码,但似乎没有任何影响导航行为的东西,但它没有按预期工作。

感谢您的帮助!

编辑:-

根据要求,我提供了用于导航的设置,例如,SellerMainScreenFragmentCreditFragment

binding.fabCredit.setOnClickListener {        
    findNavController().
        navigate(R.id.action_sellerListScreenFragment_to_creditFragment)
}

这里我使用 FABSellerMainScreenFragment 导航到 CreditFragment

但是在导航到 CreditFragment 后,在按下后退按钮时,应用程序只是退出而不是导航回来。

我想你忘了将 app:defaultNavHost="true" 添加到主 Activity 布局中的 NavHostFragment

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

来自文档:

The app:defaultNavHost="true" attribute ensures that your NavHostFragment intercepts the system Back button. Note that only one NavHost can be the default. If you have multiple hosts in the same layout (two-pane layouts, for example), be sure to specify only one default NavHost.

https://developer.android.com/guide/navigation/navigation-getting-started