Navigation structure:
MainActivity
|- nav_root
|- HomeFragment
|- AuthNestedGraph
| |- nav_auth
| | |-BeforeOtpFragment(home)
| | |-OtpFragment
|
|- ProfileNestedGraph
| |- nav_prfole
| | |-ProfileFragmentOne(home)
| | |-ProfileFragmentTwo
I can navigate from HomeFragment to BeforeOtp(nav_auth home), toProfileOne(nav_profile home). Also i can navigate from any auth fragment to toProfileOne, or from profile fragments to BeforeOtp by global id
But how navigate to child fragment that is not set home from fragment at another nested graph/nav file? Like to OtpFragment/ProfileTwoFragment. How to change navcontroller?
When trying global i got an exception: "cannot be found from the current destination Destination"
I can do it by deeplinks but it is not solution im looking for.
Turned out that answer was pretty simple, but hope it can help someone.
So, we want navigate from any Auth fragment from nav_auth to ProfileTwo from nav_profile. What we do:
<action
android:id="@+id/action_global_profileTwoFragment"
app:destination="@id/profileTwoFragment"
app:popUpTo="@id/profileTwoFragment"
app:popUpToInclusive="true"
app:launchSingleTop="false" />
override fun routeToProfileTwoFragment() {
val navController = fragment.findNavController()
navController.setGraph(R.navigation.nav_profile)
navController.navigate(R.id.action_global_profileTwoFragment)
}
override fun routeMain() {
val navController = fragment.findNavController()
navController.setGraph(R.navigation.nav_root)
navController.navigate(R.id.action_global_home)
}