[英]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
.在这里,我使用FAB从
SellerMainScreenFragment
导航到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 yourNavHostFragment
intercepts the system Back button.app:defaultNavHost="true"
属性确保您的NavHostFragment
拦截系统后退按钮。 Note that only oneNavHost
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 defaultNavHost
.如果您在同一布局中有多个主机(例如,双窗格布局),请务必仅指定一个默认
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.