1

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_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

navigation

<?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_graphs"
    app:startDestination="@id/mainFragment">
    <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" />
    </fragment>
    <!-- other fragments here...  -->
    <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>
    <fragment
        android:id="@+id/profileFragment"
        android:name="com.my.app.main.ProfileFragment"
        android:label="fragment_profile"
        tools:layout="@layout/fragment_profile" >
    </fragment>
</navigation>

SettingsFragment.kt (redirect code)

when (position) {
    0 -> {
        //....
    }
    1 -> {
        navController.navigate(R.id.action_settingsFragment_to_profileFragment)
    }
    2 -> {
        //...
    }
}

one

Any idea?

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

private fun GenerateItems(){
    when (position) {
        0 -> {
            //
        }
        1 -> {
            this@SettingsFragment.navigateTo(ProfileFragment())
        }
        2 -> {
            //
        }
    }
}

private fun navigateTo(fragment: Fragment) {
    val transaction =  (activity as FragmentActivity).supportFragmentManager
            .beginTransaction()
            .replace(R.id.fragmentFragmentId, fragment)

    transaction.commit()
}

Any idea about back button issue?

mafortis
  • 6,750
  • 23
  • 130
  • 288
  • Did you try it with action, using directions? – Eyosiyas Apr 10 '21 at 08:40
  • @Eyosiyas what do you mean? – mafortis Apr 10 '21 at 08:41
  • The one you use to pass data or arguments. – Eyosiyas Apr 10 '21 at 08:46
  • NavDirections action = SpecifyAmountFragmentDirections .actionSpecifyAmountFragmentToConfirmationFragment(); Navigation.findNavController(view).navigate(action); – Eyosiyas Apr 10 '21 at 08:46
  • @Eyosiyas not sure if I understand you correctly but I have something like this as well `override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) navController = Navigation.findNavController(view) }` – mafortis Apr 10 '21 at 08:50
  • Check this link https://developer.android.com/guide/navigation/navigation-getting-started#ensure_type-safety_by_using_safe_args – Eyosiyas Apr 10 '21 at 08:52
  • @Eyosiyas thank you but I really don't understand what `SpecifyAmountFragmentDirections` or `actionSpecifyAmountFragmentToConfirmationFragment` are referring to. – mafortis Apr 10 '21 at 08:54
  • When you use Safe Args it will automatically generate for you. – Eyosiyas Apr 10 '21 at 08:57
  • @Eyosiyas is that possible that you share an answer? cause honestly I don't see this conversation getting any outcome for solving my issue. – mafortis Apr 10 '21 at 08:58
  • https://stackoverflow.com/a/60140498/10944045 – Eyosiyas Apr 10 '21 at 09:01

2 Answers2

2

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

Zain
  • 37,492
  • 7
  • 60
  • 84
0
  if (findNavController().currentDestination?.id != R.id.addressDeleteBottonSheetFragment) {
            findNavController().navigate(
                AddAddressFragmentDirections.actionAddressFragmentToAddressDeleteBottonSheetFragment(
                    selectedAddressId
                )
            )
        }