14

I have main navigation: SplashFragment -> RegistrationFragment -> RootFragment

<fragment
    android:id="@+id/splashFragment"
    android:name="com.low6.low6.features.splash.SplashFragment"
    android:label="Splash"
    tools:layout="@layout/fragment_splash" >
    <action
        android:id="@+id/action_next"
        app:clearTask="true"
        app:destination="@id/registrationFragment" />
</fragment>

<fragment
    android:id="@+id/registrationFragment"
    android:name="com.low6.low6.features.registration.RegistrationFragment"
    android:label="Register">
    <action
        android:id="@+id/action_next"
        app:clearTask="true"
        app:destination="@id/rootFragment" />
</fragment>

<fragment
    android:id="@+id/rootFragment"
    android:name="com.low6.low6.core.RootFragment"
    android:label="@string/home"
    tools:layout="@layout/fragment_root" />

And I have nested registration navigation: RegistrationPersonalFragment -> RegistrationContactFragment -> RegistrationSecurityFragment

<fragment
    android:id="@+id/registrationPersonalFragment"
    android:name="com.low6.low6.features.registration.RegistrationPersonalFragment"
    android:label="Register">

    <action
        android:id="@+id/action_next"
        app:destination="@+id/registrationContactFragment" />
</fragment>

<fragment
    android:id="@+id/registrationContactFragment"
    android:name="com.low6.low6.features.registration.RegistrationContactFragment"
    android:label="Register">

    <action
        android:id="@+id/action_next"
        app:destination="@+id/registrationSecurityFragment" />
</fragment>

<fragment
    android:id="@+id/registrationSecurityFragment"
    android:name="com.low6.low6.features.registration.RegistrationSecurityFragment"
    android:label="Register">

    <action
        android:id="@+id/action_next"
        app:destination="@+id/rootFragment" />
</fragment>

How to redirect from the last nested RegistrationSecurityFragment to RootFragment using Jetpack Navigation component?

Currently

<action
    android:id="@+id/action_next"
    app:destination="@+id/rootFragment" />

And

   navigateTo(R.id.action_next)

Gives me

    java.lang.IllegalArgumentException: navigation destination com.xxx:id/rootFragment referenced from action com.xxx:id/action_next is unknown to this NavController
    at androidx.navigation.NavController.navigate(NavController.java:691)
    at androidx.navigation.NavController.navigate(NavController.java:648)
    at androidx.navigation.NavController.navigate(NavController.java:634)
    at com.xxx.core.BaseFragment.navigateTo(BaseFragment.kt:73)
    at com.xxx.core.BaseFragment.navigateTo$default(BaseFragment.kt:66)
    at com.xxx.features.registration.RegistrationSecurityFragment$epoxyController$1$$special$$inlined$button$lambda$1.onClick(RegistrationSecurityFragment.kt:106)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

7 Answers 7

10

If you have more than one navigation graph, please make sure you're using the right navigation controller. Using Navigation.findNavController(view) in some cases you might need to get your root view to get the root's navigation. Hope, this'll help.

2
  • 5
    How to get root view? Nov 7, 2019 at 18:13
  • @IgorGanapolsky you can pass your host's navController to your child fragment maybe? Aug 9, 2022 at 7:17
8

When you have nested NavControllers, findNavController() return only last. To get previous navControllers, you can traverse up using parentFragment property.

Extensions with this approach:

// find all nav controllers from closest to farest
fun Fragment.findAllNavControllers(): List<NavController> {
    val navControllers = mutableListOf<NavController>()
    var parent = parentFragment
    while (parent != null) {
        if (parent is NavHostFragment) {
            navControllers.add(parent.navController)
        }
        parent = parent.parentFragment
    }
    return navControllers
}

// find one nav controller by fragment id
fun Fragment.findNavControllerById(@IdRes id: Int): NavController {
    var parent = parentFragment
    while (parent != null) {
        if (parent is NavHostFragment && parent.id == id) {
            return parent.navController
        }
        parent = parent.parentFragment
    }
    throw RuntimeException("NavController with specified id not found")
}

And usage:

findAllNavControllers()[2]
findNavControllerById(R.id.navHostFragment)
2
  • The second extension function was what I needed. Thanks. Aug 10, 2021 at 11:14
  • Second extension function works, but for some reason the actual id is different than the resource id Oct 10, 2021 at 5:03
1

In your code, you can pass the resource ID of the global action to the navigate() method for each UI element.

your_button.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
       Navigation.findNavController(view).navigate(R.id.main_fragment);
   }
});
2
  • 7
    It doesn't work, I'm afraid java.lang.IllegalArgumentException: navigation destination com.xxx:id/rootFragment is unknown to this NavController
    – jakub
    Oct 10, 2018 at 9:16
  • I can confirm getting same error as @qbait in my app
    – aksh1618
    Feb 2, 2019 at 20:28
1

Thanks to @Vladimir answer I came up with this solution

val mainNavView = requireActivity().findViewById<View>(R.id.mainNavFragment)
Navigation.findNavController(mainNavView).navigate(R.id.action_next)
2
  • 1
    What is mainNavFragment? Nov 7, 2019 at 18:13
  • mainNavFragment is probably the Fragment which hosts the nested navigation. Looks like he's recovering the top most nav host fragment and using it's actions directly from the nested fragment.
    – Machado
    Feb 26, 2020 at 21:13
0

You can do it like this ;

in Child Fragment

// parentFragment = getParentFragment() for java
(parentFragment as MyParentFragment).myNavigationHandler(myArguments)

in Parent Fragment

fun myNavigationHandler(myArguments) {
    Navigation.findNavController(binding.root)
        .navigate(MyFragmentDirections.actionMyAction(myArguments))
}
0
findNavController().navigate(HostFragmentDirections.actionHostToOtherFragment())

HostFragment is just an example, which should be the name of host of your nested fragment. For everyone struggling with navigating to somewhere from your nested fragment, this works.

0

There is already a way given by Navigation class. I have the same design you have. I am using below line to navigation to top fragment

Navigation.findNavController(requireActivity(),R.id.nav_host_fragment).navigate(R.id.homeFragment)

where R.id.nav_host_fragment is fragmentcontainerview id which is present in root activity like below

<androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:clipToPadding="false"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toTopOf="@+id/bottomNav"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/nav_graph" />

Don't forget to import the correct package

import androidx.navigation.Navigation

Note : The id of root fragment container and child fragment container must be different. So it does not clash and find the correct NavController.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.