12

In our user requirement we have a flow like this.

enter image description here

I have searched the following:

https://developer.android.com/guide/navigation/navigation-principles

Circular Reference with Nested Nav Graphs

But nothing can help...

What I have done?

Currently I split 2 graph and add duplicate Fragment C in Graph2. Also, I change the graph start destination to Fragment C in graph 1 programmatically only I clicked from graph2. This is an ugly solution But I have no idea how to solve this issue.

My question is : Is there any way to do it rather making a duplicate destination and changing the start destination?

Graph 1:

<include app:graph="@navigation/graph2" />
<dialog
        android:id="@+id/fragmentA"
        android:label="Fragment A"
        tools:layout="@layout/fragmentA"/>
<dialog
        android:id="@+id/fragmentB"
        android:label="Fragment B"
        tools:layout="@layout/fragmentB">
    <action
            android:id="@+id/NavigateToGraph2"
            app:destination="@id/graph2">
</dialog>
<dialog
        android:id="@+id/fragmentC"
        android:label="Fragment C"
        tools:layout="@layout/fragmentC"/>
<dialog
        android:id="@+id/fragmentD"
        android:label="Fragment D"
        tools:layout="@layout/fragmentD"/>
<dialog
        android:id="@+id/fragmentE"
        android:label="Fragment E"
        tools:layout="@layout/fragmentE"/>

Graph 2

<dialog
        android:id="@+id/fragmentF"
        android:label="Fragment F"
        tools:layout="@layout/fragmentF"/>
<dialog
        android:id="@+id/fragmentG"
        android:label="Fragment G"
        tools:layout="@layout/fragmentG"/>
<dialog
        android:id="@+id/fragmentC"
        android:label="Fragment C"
        tools:layout="@layout/fragmentC"/>
Long Ranger
  • 5,888
  • 8
  • 43
  • 72
  • I don't know if android supports this as of now, but the following might be useful: https://stackoverflow.com/questions/51263301/multi-module-navigation-with-architecture-components – Urmzd Dec 03 '20 at 18:51
  • Thx a lot for the advice. However, the fragment C would received enum object and parcelable object from the source destination. Would it be possible to pass these 2 type by deeplink? – Long Ranger Dec 04 '20 at 02:10
  • 1
    Which one is your start destination? – Sam Chen Dec 07 '20 at 17:20
  • Oh sorry I didn't add the start destination in graph 1. Fragment A,B,C,D,E are just the part of the whole navigation. You can assume there is a Fragment AA as the start destination in graph1. In Graph 2, Fragment F is the start destination – Long Ranger Dec 08 '20 at 01:30
  • I would suggest you make Fragment C abstract to share your logic into inheriting fragments C1 & C2 where C1 is part of graph 1 while C2 is part of graph 2. – Jeel Vankhede Dec 09 '20 at 06:26
  • @JeelVankhede This could make the issue more complicated. It will create more unnecessary classes and xmls for every circular navigation. Also, it is bad for maintainence. – Long Ranger Dec 09 '20 at 12:39

1 Answers1

1

I think it is better to create a graph for fragment F and break the circular loop.

Amin Bahiraee
  • 538
  • 10
  • 20
  • 1
    How does it solve the circular loop? The new created graph for fragment should also include Fragment G as the destination – Long Ranger Dec 08 '20 at 01:27