DEV Community

Mohamed Shalan
Mohamed Shalan

Posted on

Android Navigation Component Issue

I had an issue in the application I am currently working on it followed the following scenario; Question on stackoverflow

There is an activity, let's call it PaymentFlowActivity. This activity has only 3 fragments; shipping, review and payment fragment and it doesn't have a NavHostFragment.

In the Shipping Fragment, I have more than one state, so I decided to add a NavHostFragment for this fragment to handle back stack for those different states.

so in my fragment layout file, I did the following


Enter fullscreen mode Exit fullscreen mode

And when I try to access the nav controller using findNavController() it throws the following exception

java.lang.IllegalStateException:xxxxxxxxxxxxx does not have a NavController set

when I searched for this exception, I didn't find any useful answers to this use case issue.

So, I went through the Navigation component code in the SDK and I found the following: when using the findNavController() with a fragment that isn't NavHostFragment or isn't within NavHostFragment this exception will be thrown.

So we have to find the nav controller by ourselves using the id of the nav host fragment defined in the fragment and the activity as follow

val navController = Navigation.findNavController(activity,
Enter fullscreen mode Exit fullscreen mode

this will get the navController of your NavHostFragment defined in the fragment layout.

You can use the navController to navigate to the desired destination

Enter fullscreen mode Exit fullscreen mode

To make it easier to get the nav controller, I made an extension function for the fragment class that enables accessing the nav controller using only the id.
Here is a snippet:

fun Fragment.getFragmentNavController(@IdRes id: Int) = activity?.let {
        return@let Navigation.findNavController(it, id)
Enter fullscreen mode Exit fullscreen mode

Then in your fragment, you can use this method to navigate to the desired destination.

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

pablosiax profile image

please java code:
val navController = Navigation.findNavController(activity,