繁体   English   中英

将参数传递给嵌套的导航架构组件图

[英]Passing argument(s) to a nested Navigation architecture component graph

如何将参数传递给嵌套的导航架构组件图?

假设我构建我的导航图以从FragmentA --> Nested导航,其中Nested包含FragmentB --> FragmentC ...

如果这是一个纯FragmentA --> FragmentB...图,我只需使用FragmentADirections.actionFragmentAToFragmentB(argument = foo)设置导航。 但是,一旦您将B --> C转换为Nested ...

那我该怎么办?

全局操作可能是一种方法,但是一旦我将嵌套图形提取到它自己的.xml ,我就没有像我想要的那样工作。 但结果证明这很简单 - 只需在代码中手动添加参数到您的操作中。

与该问题相关的一个示例是:

将嵌套图保存到nested_graph.xml ,它看起来像

<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>

要将参数从不同的图形传递给nested_graph.xml ,请说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>

换句话说,只需将相同的<argument ... />root_graph操作中,就像您希望在nested_graph接收到的nested_graph

如下使用全局操作并传递参数。

<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/mobile_navigation"
        app:startDestination="@id/mainFragment">


<activity android:id="@+id/playbackActivity"
          android:name="com.cinderellaman.general.ui.activities.PlaybackActivity"
          android:label="activity_playback"
          tools:layout="@layout/activity_playback">
    <argument android:name="videoId"
              app:nullable="true"/>
    <argument android:name="position"
              app:argType="integer"
              android:defaultValue="1"/>
</activity>
<action android:id="@+id/action_global_playbackActivity"
        app:destination="@id/playbackActivity"/></navigation>

实际上,这非常简单,您只需要在nested_nav 中添加您的参数,例如

 <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">
            </action>
        </fragment>
        <navigation
            android:id="@+id/nested_graph"
            app:startDestination="@id/fragmentB"
                ...>
            <argument
                android:name="foo"
                app:argType="integer"/>
            <fragment
            android:id="@+id/fragmentB"
                ...>
            <argument
            android:name="foo"
            app:argType="integer"/>
            <action
                ... // navigation action to FragmentC />
                </fragment>
            
            <fragment ...  // FragmentC stuff
        </navigation>
    </navigation>

或者您可以通过捆绑发送您的数据

要解决此问题,您需要执行以下操作:

  1. <navigation>块的Included Graph中,将目的地添加到@id/included_fragment
<action
    android:id="@+id/open_included_fragment"
    app:destination="@id/included_fragment"/>
  1. Main Graph中,确保您想要的<action> id 与Included Graph<action>的 id 相同,在您的情况下它应该保持不变。

此外,要验证目的地(如下所示),您可以从Main Graph中的<action> id 中删除+号,因为在这种情况下,它应该使用Included Graph中的<action>的 id。

总的来说你的代码应该是这样的:

主图

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_graph"
    app:startDestination="@id/main_fragment">

    <fragment
         android:id="@+id/main_fragment"
         android:name="com......MainFragment">

        <action
            android:id="@id/open_included_fragment"
            app:destination="@id/included_graph"/>
    </fragment>

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

</navigation>

包含图

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/included_graph"
    app:startDestination="@id/included_fragment">
    
    <action
        android:id="@+id/open_included_fragment"
        app:destination="@id/included_fragment"/>
    
    <fragment
        android:id="@+id/included_fragment"
        android:name="com......IncludedFragment">

        <argument
            android:name="some_argument"
            app:argType="integer" />
    </fragment>

</navigation>

如果您不想为嵌套图形创建单独的 xml,您可以在操作中覆盖目标参数,如 android 开发人员在此处所述 我只是测试它是否与导航图视图模型范围一起使用,并且效果很好。 我正在使用导航组件的2.2.0-alpha03 版 将这些参数添加到 action action_inboxFragment_to_conversation_graph 后,现在 InboxFragmentDirections.ActionInboxFragmentToConversationGraph 已正确生成。

<?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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM