【发布时间】: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)
}
这里我使用 FAB 从SellerMainScreenFragment 导航到CreditFragment。
但导航到CreditFragment 后,按下返回按钮后,应用程序将退出而不是返回。
【问题讨论】:
-
检查this
-
发布您的设置以及您如何导航到此目的地
-
@GabrieleMariotti 我已经按照你的要求编辑了这个问题,希望它能提供一些见解。
标签: android kotlin android-architecture-navigation