![](/img/trans.png)
[英]Navigation Architecture Component- Passing argument data to the startDestination with Fragment
[英]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>
或者您可以通過捆綁發送您的數據
要解決此問題,您需要執行以下操作:
<navigation>
塊的Included Graph
中,將目的地添加到@id/included_fragment
:<action
android:id="@+id/open_included_fragment"
app:destination="@id/included_fragment"/>
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.