从特定片段导航到另一个片段后,无法使用“后退”按钮返回

问题描述

在我的应用中,导航流程如下

m.addConstrs(y[t]==(x[t]+z[t])/(x[t]*2+z[t]*5),for t in T)

现在,当应用尝试从AuthFragment --> SellerMainScreenFragment -----> pay_nav_graph (Nested Graph) | |----> CreditFragment--------- | | |----> CreditAddFragment <---| | |-------> TransactionResultFragment 导航到SellerMainScreenFragment时,一切正常,并且按下后一个片段中的“后退”按钮,该应用将返回前一个。导航代码为

CreditAddFragment

但是,当应用程序从adapter.wasAddBalPressed.observe(viewLifecycleOwner,Observer { if (it != NO_VAL) findNavController().navigate(SellerMainScreenFragmentDirections.actionSellerListScreenFragmentToCreditAddFragment(it)) }) 导航到CreditFragment时,在按下CreditAddFragment中的后退按钮后,该应用程序不会返回上一个片段。代码如下:

CreditAddFragment

我尝试像这样在val action = CreditFragmentDirections.actionCreditFragmentToCreditAddFragment(viewModel.userSellerPhoneNo) findNavController().navigate(action) 上设置自定义onBackPressedCallback

OnBackPressedDispatcher

但是即使是requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) { findNavController().popBackStack() remove() } ,它也不会返回上一个片段,但是对于CreditFragment,它会按预期工作。

我还正确设置了SellerMainScreenFragment中的app:defaultNavHost = true

在这一点上,我尝试清理项目,使缓存无效,冷启动模拟器,并尝试进行调试,但即使如此,我也找不到任何可疑的东西。

因此,如果有人可以提供帮助,请尝试这样做。谢谢。

编辑1 :-

这是我当前在应用程序中使用的导航xml文件

NavHostFragment

编辑2 : 即使在明确调用之后

<?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
        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"
            app:popUpTo="@id/authFragment"
            app:popUpToInclusive="true" />
    </fragment>

    <fragment
        android:id="@+id/sellerListScreenFragment"
        android:name="com.example.paylater.ui.SellerMainScreenFragment"
        android:label="SellerListScreenFragment"
        tools:layout="@layout/fragment_main_screen">
        <action
            android:id="@+id/listFragment_to_creditFragment"
            app:destination="@id/creditFragment" />
        <action
            android:id="@+id/action_sellerListScreenFragment_to_pay_nav_graph"
            app:destination="@id/pay_nav_graph" />
        <action
            android:id="@+id/action_sellerListScreenFragment_to_creditAddFragment"
            app:destination="@id/creditAddFragment" />
    </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_self"
            app:destination="@id/creditFragment" />-->
        <action
            android:id="@+id/action_creditFragment_to_creditAddFragment"
            app:destination="@id/creditAddFragment" />
    </fragment>


    <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" />
        <argument
            android:name="sellerPhoneNo"
            app:argType="string" />

    </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" />
        <argument
            android:name="sellerPhoneNo"
            app:argType="string" />
    </fragment>

    <action
        android:id="@+id/action_global_sellerListScreenFragment"
        app:destination="@id/sellerListScreenFragment" />

    <navigation
        android:id="@+id/pay_nav_graph"
        app:startDestination="@id/payFragment">

        <argument
            android:name="sellerPhoneNo"
            app:argType="string" />
        <fragment
            android:id="@+id/payFragment"
            android:name="com.example.paylater.ui.PayFragment"
            android:label="PayFragment">
            <argument
                android:name="sellerPhoneNo"
                app:argType="string" />
            <action
                android:id="@+id/action_payFragment_to_processPaymentFragment"
                app:destination="@id/processPaymentFragment" />
            <action
                android:id="@+id/action_payFragment_self"
                app:destination="@id/payFragment" />
        </fragment>
        <fragment
            android:id="@+id/processPaymentFragment"
            android:name="com.example.paylater.ui.ProcessPaymentFragment"
            android:label="ProcessPaymentFragment" />
    </navigation>
</navigation>

findNavController().popBackStack(R.id.creditFragment,false) 中,它仍然没有返回上一个片段(onBackPressedCallback

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)