Ham*_*eni 9 android android-navigation android-jetpack android-architecture-navigation
在我正在构建的应用程序中,我使用了单一活动架构,并决定使用 Google 的新导航组件来浏览应用程序。
虽然它显示出很大的希望,但它有一些缺点,我的问题是关于其中之一。
假设我们有三个按顺序导航的片段,除了当我们在第三个片段上单击后退按钮时我们想返回到第一个片段。这是它的过程:
<?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_graph.xml"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="com.hmomeni.navisample.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first" >
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.hmomeni.navisample.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" >
<action
android:id="@+id/action_secondFragment_to_thirdFragment"
app:destination="@id/thirdFragment"
app:popUpTo="@+id/firstFragment" />
</fragment>
<fragment
android:id="@+id/thirdFragment"
android:name="com.hmomeni.navisample.ThirdFragment"
android:label="fragment_third"
tools:layout="@layout/fragment_third" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
这里的问题是,当我想第二次重复导航时,会发生异常告诉我:
java.lang.IllegalArgumentException: 导航目的地 com.hmomeni.navisample:id/action_firstFragment_to_secondFragment 对这个 NavController 是未知的
进一步调查显示,在点击返回按钮并返回到第一个片段时,navController.currentDestination仍然指的是ThirdFragment哪个是错误的,应该是FirstFragment。
对此的任何帮助表示赞赏。
我遇到了与此问题类似的问题,但使用循环导航,其中没有弹出后退堆栈。从 C --> A 导航时,我错误地将参数设置navigate(int resId)为
R.id.fragmentC
而不是使用类似的动作
R.id.action_c_to_a
我找到了一种解决方法,但我不会称之为解决方案。
要解决此问题,可以创建一个针对ThirdFragment其自身的操作,然后检查navController.currentDestination何时调用导航。
由于缺乏真正的解决方案,我不会接受这个答案。
| 归档时间: |
|
| 查看次数: |
12580 次 |
| 最近记录: |