netcyrax
netcyrax

Reputation: 1099

Jetpack navigation initiating previous screen

I have this simple navigation setup consisting of 3 screens:

Screen 1 -> Screen 2 -> Screen 3
    |______________________^

Screen 2 is the signup form that can be skipped when completed once.

I have this weird issue with a Jetpack navigation graph where when navigating from Screen 2 -> 3 the app crashed because of Screen 1 (!) onViewCreated() activated and navigation directions cannot be found:

    java.lang.IllegalArgumentException: navigation destination 
com.example.app:id/action_screen1_to_screen3 is unknown to this NavController

My navigation graph:

   <fragment
        android:id="@+id/screen1"
        android:name="Screen1Fragment"
        android:label="Screen 1"
        tools:layout="@layout/fragment_screen_1">
        <action
            android:id="@+id/action_screen1_to_screen2"
            app:destination="@id/screen2"
            app:launchSingleTop="false"/>
        <action
            android:id="@+id/action_screen1_to_screen3"
            app:destination="@id/screen3"
            app:launchSingleTop="false"
            app:popUpTo="@id/screen1"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/screen2"
        android:name="Screen2Fragment"
        android:label="Screen 2"
        tools:layout="@layout/fragment_screen_2">
        <action
            android:id="@+id/action_screen2_to_screen3"
            app:destination="@id/screen3"
            app:launchSingleTop="false"
            app:popUpTo="@id/screen2"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/screen3"
        android:name="Screen3Fragment"
        android:label="Screen 3"
        tools:layout="@layout/fragment_screen_3">
    </fragment>

I am using auto-generated NavDirections so it's not a matter of using the wrong resource id.

Navigation code:

// Screen 1
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if (isLoggedIn) {
            view?.findNavController()
                ?.navigate(Screen1FragmentDirections.screen1ToSelectScreen3())
        } else {
            signInButton.setOnClickListener {
                val action =
                    Screen1FragmentDirections.screen1ToScreen2()
                view?.findNavController()?.navigate(action)
            }
        }
    }

// Screen 2
view?.findNavController()?.navigate(Screen2FragmentDirections.screen2ToScreen3())

My Gradle imports:

    def nav_version = "2.3.0-alpha04"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

Any help is appreciated, thanks!

Upvotes: 0

Views: 289

Answers (1)

netcyrax
netcyrax

Reputation: 1099

Not sure why it's happening but the solution I found was instead of popUpTo until the current screen, is to popUpTo the entire navigation graph:

[...]
app:popUpTo="@id/nav_graph"
app:popUpToInclusive="true"
[...]

Upvotes: 1

Related Questions