0

I'm currently trying to transition to a Fragment in navigation2.xml to navigation.xml. I thought Reference other navigation graphs with would do but doesn't work. If there is a sample or hints about transitioning between fragments of different navigation.xml files , I would love to hear from you!

<!-- (root) navigation.xml -->
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
    app:startDestination="@id/fragment">

    <include app:graph="@navigation/included_graph" />

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.myapplication.BlankFragment"
        android:label="Fragment in Root Graph"
        tools:layout="@layout/fragment_blank">
        <action
            android:id="@+id/action_fragment_to_second_graph"
            app:destination="@id/second_graph" />
    </fragment>

    ...
</navigation>

second navigation file

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/second_graph"
    app:startDestination="@id/includedStart">

    <fragment
        android:id="@+id/includedStart"
        android:name="com.example.myapplication.IncludedStart"
        android:label="fragment_included_start"
        tools:layout="@layout/fragment_included_start" />
</navigation>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Nancy
  • 129
  • 2
  • 13
  • add some code here what you tried doing. – Anmol Jul 02 '19 at 04:17
  • There is only two default navigation.xml and navigation2.xml. Since I don't know how to connect the basic, nothing is done. I need sample and hints because I can7t find a tutorial about it – Nancy Jul 02 '19 at 04:23

1 Answers1

1

As per the Design navigation graphs documentation:

[Nested graphs] also provide a level of encapsulation — destinations outside of the nested graph do not have direct access to any of the destinations within the nested graph. Instead, they should navigate() to the nested graph itself, where the internal logic can change without affecting the rest of the graph.

Therefore you can use navigate(R.id.second_graph) without issue from anywhere in navigation.xml, but you can't directly access anything within that other graph.

Note that the one exception to this is navigating by URI, which allows you to navigate to any destination in your graph (whether nested or not) by navigating using an implicit deep link.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443