android-navigationandroid-navigation-graph

how to make action from included graph?


I can make action from nested graph to any destination, but apprently I can't make action from included other file.

i thought included graph and nested graph is almost same. how can I make action from included graph?


Solution

  • You can create a global action in the main navigation graph Like:

    <navigation xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_graph"
        app:startDestination="@id/x">
    
        <fragment
            android:id="@+id/x"
            android:name="x"/>
    
        <include app:graph="@navigation/x" />
    
        <fragment
            android:id="@+id/firstFragment"
            android:name="x"/>
    
        <action
            android:id="@+id/action_global_to_firstFragment"
            app:destination="@id/firstFragment" />
    
    </navigation>
    

    And then call it with the id:

    navController.navigate(R.id.action_global_to_firstFragment)
    

    The navController must be the defined in the ActivityContainer, if it generate a destination error, maybe you aren't using the main navController