【问题标题】:Pressing back button exits the app instead of navigating to the previous screen while using navigation component使用导航组件时按下后退按钮退出应用程序而不是导航到上一个屏幕
【发布时间】:2020-07-24 11:08:09
【问题描述】:

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

我的导航图如下所示:

<?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 回调来解决此问题,但除了会导致一些奇怪的行为之外,对每个片段都执行此操作也很乏味,而且对于应该处理的事情来说似乎工作量太大通过导航组件。

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

感谢您的帮助!

编辑:-

如被问及,我提供了用于从 SellerMainScreenFragment 导航到 CreditFragment 的设置

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

这里我使用 FABSellerMainScreenFragment 导航到CreditFragment

但导航到CreditFragment 后,按下返回按钮后,应用程序将退出而不是返回。

【问题讨论】:

  • 检查this
  • 发布您的设置以及您如何导航到此目的地
  • @GabrieleMariotti 我已经按照你的要求编辑了这个问题,希望它能提供一些见解。

标签: android kotlin android-architecture-navigation


【解决方案1】:

我认为您忘记在您的主 Activity 布局中将 app:defaultNavHost="true" 添加到 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" />

来自文档:

app:defaultNavHost="true" 属性确保您的NavHostFragment 拦截系统后退按钮。请注意,只有一个NavHost 可以是默认值。如果您在同一布局中有多个主机(例如,双窗格布局),请务必仅指定一个默认 NavHost

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

【讨论】:

  • 非常感谢。那确实是我错过的事情。我几乎放弃了正确的导航,但多亏了你,我才能让它工作。我不敢相信我已经放弃了这个问题,因为我做了这么愚蠢的事情。除此之外,如果我再次打扰您,后退按钮在全局导航图中正常工作,但在嵌套图中,按下后退没有任何作用。因此,如果我再次缺少某些东西,您能否再指出一次。再次感谢您的帮助。
  • 很难说。我建议就这个特定问题提出另一个问题。
  • 好的,我明白了。我想我会尝试修复它。如果无法解决,我会继续问另一个问题。
猜你喜欢
  • 2020-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
  • 2020-03-23
相关资源
最近更新 更多