Passing argument(s) to a nested Navigation archite

2020-02-26 07:40发布

问题:

How does one pass argument(s) to a nested Navigation architecture component graph?

Let's say I construct my navigation graph to navigate from FragmentA --> Nested, where Nested contains FragmentB --> FragmentC...

If this was a pure FragmentA --> FragmentB... graph, I would just set up the navigation with FragmentADirections.actionFragmentAToFragmentB(argument = foo). But that action takes zero arguments as soon as you turn B --> C into Nested...

So what am I supposed to do?

回答1:

Global actions might be a way but I didn't get that working as I wanted once I extracted the nested graph to its own .xml. But it turned out to be embarrassing simple - just add the arguments manually in code, to your action.

An example related to the question would be:

Save the nested graph to nested_graph.xml, it will look something like

<navigation
    android:id="@+id/nested_graph"
    app:startDestination="@id/fragmentB"
    ...>

    <fragment 
        android:id="@+id/fragmentB"
        ...>
        <argument
            android:name="foo"
            app:argType="integer"/>
        <action 
            ... // navigation action to FragmentC />
    </fragment>

    <fragment ...  // FragmentC stuff
</navigation>

To pass arguments to nested_graph.xml from a different graph, say root_graph.xml do

<navigation
    android:id="@+id/root_graph"
    app:startDestination="@id/fragmentA"
    ...>

    <fragment 
        android:id="@+id/fragmentA"
        ... >
        <action
            android:id="@+id/action_fragmentA_to_nested_graph"
            app:destination="@id/nested_graph">
            <argument
                android:name="foo"
                app:argType="integer"/>
        </action>
    </fragment>
    <include app:graph="@navigation/nested_graph"/>
</navigation>

In other words, just add the same <argument ... /> to the root_graph action as you expect to receive in the nested_graph.



回答2:

If you do not want to create a separated xml for the nested graph you can override destination argument in an action as android developers says here. I just test it for use with navigation graph view model scope and it worked perfectly. I am using Version 2.2.0-alpha03 of navigation component. After added those params to the action action_inboxFragment_to_conversation_graph, now the InboxFragmentDirections.ActionInboxFragmentToConversationGraph is generated correctly.

<?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/messages_graph"
    app:startDestination="@id/inboxFragment">
    <fragment
        android:id="@+id/inboxFragment"
        android:name="com.wlpr.docfinder.ui.fragment.InboxFragment"
        android:label="fragment_inbox"
        tools:layout="@layout/fragment_inbox" >
        <action
            android:id="@+id/action_inboxFragment_to_conversation_graph"
            app:destination="@id/conversation_graph">
            <argument
                android:name="participantName"
                android:defaultValue="Conversation"
                app:argType="string"
                app:nullable="true" />
            <argument
                android:name="documentsCount"
                android:defaultValue="1"
                app:argType="integer" />
        </action>
    </fragment>
    <navigation
        android:id="@+id/conversation_graph"
        android:label="conversationGraph"
        app:startDestination="@id/conversationFragment">
        <fragment
            android:id="@+id/conversationFragment"
            android:name="com.wlpr.docfinder.ui.fragment.ConversationFragment"
            android:label="fragment_conversation"
            tools:layout="@layout/fragment_conversation">
            <action
                android:id="@+id/action_conversationFragment_to_reportingDetailsFragment"
                app:destination="@id/reportingDetailsFragment" />
            <argument
                android:name="participantName"
                android:defaultValue="Conversation"
                app:argType="string"
                app:nullable="true" />
            <argument
                android:name="documentsCount"
                android:defaultValue="1"
                app:argType="integer" />
        </fragment>
        <!-------- more fragments... -------->
</navigation>