Navigation component pop behavior is not working as it should
I was having an issue similar to this question but with circular navigation, where the back stack was not being popped. When navigating from C --> A, I was mistakenly setting the parameter for navigate(int resId)
as
R.id.fragmentC
instead of using an action like
R.id.action_c_to_a
Related videos on Youtube
Comments
-
2hamed almost 2 years
Inside an app I'm building I have used the single activity architecture and decided to use Google's new Navigation component to navigate around the app.
Though it's showing great promise, it has some drawbacks which my question is about one of them.Assume that we have three fragments which are navigated in order, except that we want to go back to the first one when back button is clicked when we are on the third fragment. Here's how it goes:
<?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>
The problem here is that when I want to repeat the navigation for a second time an exception occurs telling me that:
java.lang.IllegalArgumentException: navigation destination com.hmomeni.navisample:id/action_firstFragment_to_secondFragment is unknown to this NavController
Further investigation shows that upon hitting back button and returning to the first fragment, the
navController.currentDestination
still refers to theThirdFragment
which is wrong and it should beFirstFragment
.Any help on this is appreciated.
-
Eric B. over 4 yearsGlad it helped!