2

I hope you're all doing well.

I have a problem about navigation graphs. You can see the structure in the image below;

enter image description here So we have 2 different navigation graphs; navigation_A and navigation_B.

I need to navigate to Fragment Y and Fragment Z from a fragment in nested graph which is navigation_B.

The problem is;

  • If I use <include> for navigation_B, I can not use it as NavHost.
  • If I use different navigation graph and add it as nested graph like above, I can not use navigation destination for Fragment Y or Fragment Z.

I need to use navigation_B as NavHost, also i need to be able to navigate to Fragment Y and Fragment Z. How can I achieve that?

Thank you!

2 Answers 2

1

Your navigation graph structure breaks the recommended navigation pattern!

But for your needs, I have a way to workaround.

Method 1: First, FragmentY and FragmentZ need to have their own deeplinks.

    <fragment
      android:id="@+id/fragY"
      android:name=".FragmentY"
      tools:layout="@layout/frag_y">
      <deepLink
        android:id="@+id/"
        app:uri="any-app://internal_navigation_frag_y" />
    </fragment>

    <fragment
      android:id="@+id/fragZ"
      android:name=".FragmentZ"
      tools:layout="@layout/frag_z">
      <deepLink
        android:id="@+id/dlink_frag_z"
        app:uri="any-app://internal_navigation_frag_z" />
    </fragment>

Then, Inside a Fragment that lives in navigation_b. Let's call it FragmentB

// navigate to FramgmentY
val deeplinkY = Uri.parse("any-app://internal_navigation_frag_y")
findNavController().navigate(deeplinkY)

// navigate to FramgmentZ
val deeplinkZ = Uri.parse("any-app://internal_navigation_frag_z")
findNavController().navigate(deeplinkZ)

You can store the string any-app://internal_navigation_frag_y and any-app://internal_navigation_frag_z in string.xml file though.

Check this out: https://developer.android.com/guide/navigation/navigation-navigate#uri

Method 2: Inside the nested graph navigation_B, define 2 global actions that point to fragmentY and fragmentZ. Since navigation_B is a NavHost, so it will know about FragmentY and FragmnentZ

Check this: https://developer.android.com/guide/navigation/navigation-global-action

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

4 Comments

Thanks a lot. The deeplink option helped. Can this work with multi-modular applications too @Hau Luu
@DenisOluka, haven't tested multi-modular yet. But theoretically, It'll work.
Hey, can you elaborate a bit more on this please > Your navigation graph structure breaks the recommended navigation pattern!
@rdias002 check the answer below. I think Pablo has already answered your question.
0

When you use nested navigation graph the inner graph should not know anything about the outside graph. In this case graph_B should not know about the existence of a FragmentY or FragmentZ. What you should do is deliver a result from your graph_B to whomever launched it. Then at that point decide whether to go FragmentY or FragmentZ.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.