Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation Component: How to solve the circular navigation with nested graph?

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"/>
like image 891
Long Ranger Avatar asked Dec 03 '20 08:12

Long Ranger


People also ask

What is NavController in Android?

NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.


Video Answer


1 Answers

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

like image 122
Amin Bahiraee Avatar answered Oct 14 '22 07:10

Amin Bahiraee