Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation component pop behavior is not working as it should [duplicate]

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:

Navigation from one to two to three

<?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 the ThirdFragment which is wrong and it should be FirstFragment.

Any help on this is appreciated.

like image 321
2hamed Avatar asked Aug 09 '18 06:08

2hamed


People also ask

What is pop up to in navigation component?

app:popUpTo tells the Navigation library to pop some destinations off of the back stack as part of the call to navigate() . The attribute value is the ID of the most recent destination that should remain on the stack.

What is popUpToInclusive?

When navigating back to destination A, we also popUpTo A, which means that we remove B and C from the stack while navigating. With app:popUpToInclusive="true", we also pop that first A off of the stack, effectively clearing it.


1 Answers

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

like image 60
Eric B. Avatar answered Nov 15 '22 17:11

Eric B.