Search code examples
androidnavigationandroid-navigation-graphandroid-safe-args

When I add the same fragment to second navigation graph, don´t recognize the Action Class


I need to add the same fragment to different navigation graphs. This is piece of code of first graph (mobile_navigation)

<fragment
    android:id="@+id/nav_contactanos"
    android:name="com.engie.mexico.micuenta.Fragments.FragmentContactanosCliente"
    tools:layout="@layout/fragment_contactanos_cliente" >
    <action
        android:id="@+id/action_nav_contactanos_to_nav_aviso_privacidad"
        app:destination="@id/nav_aviso_privacidad" />
    <action
        android:id="@+id/action_nav_contactanos_to_nav_terminos_condiciones"
        app:destination="@id/nav_terminos_condiciones" />
    <action
        android:id="@+id/action_nav_contactanos_to_nav_mensaje_usuario"
        app:destination="@id/nav_mensaje_usuario" />
</fragment>

The second fragment of code in second navigation graph (navigation_inicial)

    <fragment
    android:id="@+id/nav_contactanos_cliente"
    android:name="com.engie.mexico.micuenta.Fragments.FragmentContactanosCliente"
    tools:layout="@layout/fragment_contactanos_cliente" >
    <action
        android:id="@+id/action_nav_contactanos_cliente_to_nav_mensaje_usuario"
        app:destination="@id/nav_mensaje_usuario" />
    <action
        android:id="@+id/action_nav_contactanos_cliente_to_nav_terminos_condiciones"
        app:destination="@id/nav_terminos_condiciones" />
    <action
        android:id="@+id/action_nav_contactanos_cliente_to_nav_aviso_privacidad"
        app:destination="@id/nav_aviso_privacidad" />
</fragment>

When I try to MakeProject, the build output shows me the error :

C:\Android\MiCuenta\app\src\main\java\com\engie\mexico\micuenta\Fragments\FragmentContactanosCliente.java:478: error: cannot find symbol
            FragmentContactanosClienteDirections.ActionNavContactanosToNavMensajeUsuario action = symbol:   class ActionNavContactanosToNavMensajeUsuario

location: class FragmentContactanosClienteDirections

But when I comment the second piece of code (navigation_inicial), the problem disappears. The thing is that I need the second piece of code because I need to recall fragment and share with it bundles, with other information and show different things... How can achieve this?

I also share the design of graphs:

mobile_navigation

mobile_navigation.xml (design)

navigation_inicial

navigation_inicial.xml (design)


Solution

  • The name of the Directions class is based on the name of the Fragment and there can only be one instance of a particular class at a time. As per this issue with Safe Args, there is no warning when you are overwriting one Directions class from one from another graph - the last one wins. This is why reusing the same fragment in a different graph invalidates the Directions class from the first graph.

    Of course, if your second graph's fragment has different actions, different arguments, or anything at all different about it, it should have a different fragment class as well - if your single fragment class were to use arguments or actions from another graph, they would fail as they would not exist in that graph.

    As mentioned in that bug:

    For a destination with an android:name, you'd want to include that android:name in only one place in your graph, referencing that shared destination from all of the places that need access to it (as a destination can access any sibling destinations of their parent graph).