[英]Android Navigation Component - external included graph receiving one argument
我正在使用 Jetpack 的導航組件。
我的主圖很大,所以我決定將它分成不同的圖並使用<include />
功能。
問題是一些包含的圖表收到 arguments,但 Android 在創建導航Directions
時似乎忽略了 arguments
IE:
主圖(簡化)
<?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">
<fragment
android:id="@+id/listFragment"
android:name="com......ListFragment">
<action
android:id="@+id/view_detail"
app:destination="@id/item_detail"/>
</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/item_detail"
app:startDestination="@id/itemDetailFragment">
<argument
android:name="item_id"
app:argType="integer" />
<fragment
android:id="@+id/itemDetailFragment"
android:name="com......ItemDetailFragment">
<argument
android:name="item_id"
app:argType="integer" />
</fragment>
[...]
</navigation>
android生成的class為
class ListFragmentDirections private constructor() {
companion object {
fun viewDetail(): NavDirections =
ActionOnlyNavDirections(R.id.view_detail)
}
}
包含的圖形接收到的參數似乎被忽略了——我無法安全地傳遞任何參數。
有解決方法嗎?
我對任何替代方案感興趣,讓我根據<include />
或不將圖表分解成更小的文件。
有一種解決方法:將參數明確包含在操作中,即
<?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">
<fragment
android:id="@+id/listFragment"
android:name="com......ListFragment">
<action
android:id="@+id/view_detail"
app:destination="@id/item_detail">
<argument
android:name="item_id"
app:argType="integer" />
</action>
</fragment>
<include app:graph="@navigation/included_graph"/>
</navigation>
要解決此問題,您需要執行以下操作:
Included Graph
中,從<navigation>
塊中刪除item_id
<argument>
。<navigation>
塊中的Included Graph
中,將目的地添加到@id/itemDetailDragment
:<action
android:id="@+id/view_detail"
app:destination="@id/itemDetailDragment"/>
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<fragment
android:id="@+id/listFragment"
android:name="com......ListFragment">
<action
android:id="@id/view_detail"
app:destination="@id/item_detail"/>
</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/item_detail"
app:startDestination="@id/itemDetailFragment">
<action
android:id="@+id/view_detail"
app:destination="@id/itemDetailDragment"/>
<fragment
android:id="@+id/itemDetailFragment"
android:name="com......ItemDetailFragment">
<argument
android:name="item_id"
app:argType="integer" />
</fragment>
</navigation>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.