简体   繁体   English

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

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

I am using the navigation architecture component in my app and whenever I navigate to a screen and then try to return to the previous screen using the back button, instead of navigating back the app exits.我在我的应用程序中使用导航架构组件,每当我导航到一个屏幕然后尝试使用后退按钮返回上一个屏幕时,而不是导航回应用程序退出。

My Navigation Graph looks like this:我的导航图如下所示:

<?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>

I've tried to fix this using a custom onBackPressedDispatcher callback, but on top of leading to some strange behaviour it is also tedious to do it for every fragment and seems to be too much work for something that should be handled by the navigation component.我尝试使用自定义的onBackPressedDispatcher回调来解决此问题,但除了导致一些奇怪的行为之外,对每个片段都执行此操作也很乏味,而且对于应该由导航组件处理的事情来说似乎工作量太大。

So is there a way to fix this problem as I have gone through my code but there doesn't seem to be anything that influences the navigation behaviour yet it doesn't work as intended.那么有没有办法解决这个问题,因为我已经完成了我的代码,但似乎没有任何影响导航行为的东西,但它没有按预期工作。

Thanks for helping out!感谢您的帮助!

Edit:-编辑:-

As asked, I am providing the setup using which I navigate from, for example, SellerMainScreenFragment to CreditFragment如被问及,我提供了用于从例如SellerMainScreenFragment导航到CreditFragment的设置

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

Here I use a FAB to navigate from SellerMainScreenFragment to CreditFragment .在这里,我使用FABSellerMainScreenFragment导航到CreditFragment

But after navigating to CreditFragment , on pressing the back button the app just exits instead of navigating back.但是在导航到CreditFragment之后,在按下后退按钮时,应用程序只是退出而不是导航回来。

I think you forgot to add app:defaultNavHost="true" to the NavHostFragment in your main Activity layout:我认为您忘记在主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" />

From the documentation:从文档中:

The app:defaultNavHost="true" attribute ensures that your NavHostFragment intercepts the system Back button. app:defaultNavHost="true"属性确保您的NavHostFragment拦截系统后退按钮。 Note that only one NavHost can be the default.请注意,只有一个NavHost可以是默认值。 If you have multiple hosts in the same layout (two-pane layouts, for example), be sure to specify only one default NavHost .如果您在同一布局中有多个主机(例如,双窗格布局),请务必仅指定一个默认NavHost

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按后退按钮退出应用程序,而不是在 WebView 中向后导航 - Pressing back button exits the app instead of navigating backwards in the WebView Android 应用程序在按下后退按钮时退出而不是导航到上一个屏幕 - Flutter - Android App Exits on Press on Back Button Instead of Navigating to Previous Screen - Flutter 按下后退按钮时,导航抽屉将打开,而不是先前的活动 - While pressing back button navigation drawer is opening instead of previous activity 导航组件 - 后退按钮退出应用 - Navigation component - back button exits the app 后退按钮退出应用程序而不是转到上一个活动 - back button exits app instead of going to previous activity Webview上的“后退”按钮退出应用程序,而不显示先前的Listview结果 - back button on webview exits app instead of displaying previous listview results Android导航架构组件-系统的后退按钮退出应用 - Android navigation architecture component - system's back button exits the app 后退按钮关闭应用程序而不是转到上一个片段 android 导航组件 - Back button closes app instead of going to previous fragment android navigation component 按下后退按钮时,应用程序将关闭,同时在单个活动中使用导航抽屉中的片段 - On pressing back button app closes while using fragments in navigation drawer in single activity 在使用 Jetpack Compose 的导航组件时,弹出由可组合表示的屏幕(从“返回堆栈”)返回上一个屏幕? - Popping off screen (from “back stack”) represented by a composable to go back to previous screen while using navigation component of Jetpack Compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM