1

enter image description here

Below is my navigation structure.

MainActivity |- MainNavHost |- Fragment 1 |- Fragment 2 | |- NestedNavHost | | |-Fragment A | | |-Fragment B |- Fragment 3 

I want to write a fragment test(launchFragmentInContainer) for Fragment 2.

I want to assert navigation from Fragment 2 to fragment 3

/* fragment2.xml */ <?xml version="1.0" encoding="utf-8"?> <androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/container" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/nested_graph" /> /* Fragment2.kt */ class Fragment2 : Fragment(R.layout.fragment2), FragmentA.Listener { override fun navigateToFragment3() { findNavController().navigate(R.id.action_fragment2_to_fragment3) } } /* FragmentA.kt */ class FragmentA : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) requireView().setOnClickListener{ (parentFragment?.parentFragment as? Listener)?.navigateToFragment3() } } } /* Fragment3.kt */ class Fragment3 : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) } } interface Listener { fun navigateToFragment3() } 

But my test fails, saying that you cant navigate from nested graph(Fragment A) to Fragment 3. please find below the error log.

java.lang.IllegalArgumentException: Navigation action/destination com.test:id/action_fragment2_to_fragment3 cannot be found from the current destination Destination(com.test:id/fragmentA) label=Additional class=com.test.info.fragmentA at androidx.navigation.NavController.navigate(NavController.java:940) at androidx.navigation.NavController.navigate(NavController.java:877) at androidx.navigation.NavController.navigate(NavController.java:1169) at com.test.navigate(NavigationClient.kt:76) at com.test.navigation.NavigationClientBase$DefaultImpls.navigate$default(NavClient.kt:79) at com.test.Fragment2.onViewCreated$lambda-1(Fragment2.kt:34) at com.test.Fragment2.$r8$lambda$hXZMY5lNDkQ-WfH6BWHRHQUX6r0(Unknown Source:0) at com.test.Fragment2$$ExternalSyntheticLambda3.onChanged(Unknown Source:4) at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133) at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151) at androidx.lifecycle.LiveData.setValue(LiveData.java:309) at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50) at androidx.lifecycle.LiveDataScopeImpl$emit$2.invokeSuspend(CoroutineLiveData.kt:99) at androidx.lifecycle.LiveDataScopeImpl$emit$2.invoke(Unknown Source:8) at androidx.lifecycle.LiveDataScopeImpl$emit$2.invoke(Unknown Source:4) 

1 Answer 1

0

Can you post your code for navigation? I think the problem is that you're trying to navigate to fragment3 from fragment A which are not on the same nav_graph, you should use inside your fragment A

requireActivity().findNavController(R.id.yourNavHost).navigate(R.id.yourAction) 

edit :https://goonlinetools.com/snapshot/code/#wxfwwgpdu7nkya8k91wnd

Sign up to request clarification or add additional context in comments.

4 Comments

I updated the question with some code snipper
I have no problem with the code you posted, are you using 2 separated nav graphs? I updated my answer with the navs i used
Yes, i am using 2 nav graphs. Exactly the same goonlinetools.com/snapshot/code/#wxfwwgpdu7nkya8k91wnd
Can you upload your project to git so I can give a rapid check?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.