Skip to content Skip to sidebar Skip to footer

Action Cannot Be Found From The Current Destination

I have very simple navigation and when I attempt to go to new fragment I get this error: java.lang.IllegalArgumentException: Navigation action/destination com.my.app:id/action_sett

Solution 1:

java.lang.IllegalArgumentException: Navigation action/destination com.my.app:id/action_settingsFragment_to_profileFragment cannot be found from the current destination Destination(com.my.app:id/mainFragment) label=fragment_main class=com.my.app.main.MainFragment

The exception means that you are trying to use an action named as action_settingsFragment_to_profileFragment that can not be found in the current destination/fragment which is MainFragment

As action name implies: It's an action that you use to move from settingsFragment to profileFragment, so can only use this action within the settingsFragment.

Note: The auto generated action name is just an indication for the source & destination, but it can mean something else if the naming is messed up.

Confirming that in navGraph:

<fragment
    android:id="@+id/settingsFragment"
    android:name="com.my.app.main.SettingsFragment"
    android:label="fragment_settings"
    tools:layout="@layout/fragment_settings" >
    <action
        android:id="@+id/action_settingsFragment_to_newOrderFragment"
        app:destination="@id/newOrderFragment" />
    <action
        android:id="@+id/action_settingsFragment_to_profileFragment"
        app:destination="@id/profileFragment" />
</fragment>

This part of navGraph is when you at the settingsFragment, and there are two actions where you can go from the settingsFragment to newOrderFragment fragment or to profileFragment; or even pop up the backstack to return to the previous fragment

If you really want to go to the SettingFragment from the MainFragment, then you need to create a new action in the MainFragment as there is no one there, like below

<fragment
    android:id="@+id/mainFragment"
    android:name="com.my.app.main.MainFragment"
    android:label="fragment_main"
    tools:layout="@layout/fragment_main" >

    <action
        android:id="@+id/action_mainFragment_to_laundriesFragment"
        app:destination="@id/laundriesFragment" />

    <action
        android:id="@+id/action_mainFragment_to_settingsFragment"
        app:destination="@id/settingsFragment" />
</fragment>

UPDATE:

I've managed to redirect to profileFragment with following code, but when I hit back button it closes the app instead of back to settingsFragment

You need to add app:popUpTo="@id/settingsFragment" and specify the fragment that you want the back stack popup to when you click the back or parent buttons

<fragment
    android:id="@+id/settingsFragment"
    android:name="com.my.app.main.SettingsFragment"
    android:label="fragment_settings"
    tools:layout="@layout/fragment_settings" >
    <action
        android:id="@+id/action_settingsFragment_to_newOrderFragment"
        app:destination="@id/newOrderFragment" />
    <action
        android:id="@+id/action_settingsFragment_to_profileFragment"
        app:popUpTo="@id/settingsFragment"
        app:destination="@id/profileFragment" />
</fragment>

To use the generated directions class: dd apply plugin: "androidx.navigation.safeargs" plugin to the gradle module level

Add the class path to the gradle app level:

buildscript {
    repositories {
        google()
    }
    dependencies {
        def nav_version = "2.3.5"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}

Then Build > Make project to generate the direction class. and In navigation use the generated class

navController.navigate(SettingsFragmentDirections.actionSettingsFragmentToProfileFragment())

Also check documentation for more info

Post a Comment for "Action Cannot Be Found From The Current Destination"