Is it possible to navigate from one navgraph to a different one on a different starting fragment? (Nested Navgraph)

41 views Asked by At

I have 2 activities, MainActivity and LearningActivity, each with their own FragmentContainerView and their own navigationGraph. For example:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.principal.MainActivity">

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/navMain"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/main_graph"
            />

</androidx.constraintlayout.widget.ConstraintLayout>

The first navgraph, called main_graph, is the first thing the users see. This one has a navigation like this:

MainActivity
|- FragmentIntro1 (Starting fragment)
   |- FragmentIntro2
   |  |- FragmentMainMenu
   |  |  |-FModule 1
   |  |  |  |-Navigates to LearningActivity
   |  |  |-FModule 2
   |  |  |-FModule 3
   |  |  |-FModule 4

As you can see, the second activity is initiated by a button on the FragmentModule1. LearningActivity has its own navgraph, called learning_graph. It has a much more complicated navigation tree so ill try to reduce it

LearningActivity
|- FSubject1
   |- FClass1
   |- FClass2
   |- FClass'n'
|- FSubject2
|- FSubject'n'

Within each one of the "Class" Fragments, there is a button to return to their respective Subject. Here comes my problem. In the "Subject" fragments, there is also a button that should allow the user to return to the main menu. However, I cant simply initiate a new MainActivity, because it starts from the Intro Fragments, and I dont want the user to go through those fragments again. It should directly go to MainMenuFragment of the main_graph.

I tried to do

val menuIntent = NavDeepLinkBuilder(requireContext())
    .setComponentName(MainActivity::class.java)
    .setGraph(R.navigation.main_graph)
    .setDestination(R.id.MainMenuFragment)
    .createPendingIntent()

But obviously this only shows that one fragment, which means the user cant do any further navigation the way I showed in the trees above.

Alternatively, I think i could simply create a "clone" of both MainActivity and main_graph but skipping the Intro Fragments, however i am absolutely sure this is in no way an efficient way of doing it.

I came across nesting navigation graphs, but all the information I was able to find is only pertaining Jetpack Compose, which Im not using for this app nor do I plan on migrating to, just good ole' xml. With that, is it possible to nest navigation graphs or achieve what I want without Jetpack Compose?

Forgot to mention, I know i could simply have all of this in only one navgraph. However, due to the scale of the app, I decided to separate them. Each one of the app "Modules" function as a separate mini educational game, for instance, one of them allows users to play both at the same time (this is made for an Android Smart Table), in order to do that, I have two completely separate FragmentContainers, using the same navgraph in one activity for that Module. However if possible I would use nested navgraphs to replicate that funcionality.

0

There are 0 answers