我正在使用Jetpack的导航组件。
我的主图非常大,因此我决定将其拆分为不同的图,并使用<include />
功能。
问题是,其中一些图表接收参数,但Android在创建导航Directions
时似乎忽略了这些参数
即:
主图(简化)
<?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 ListFragmentDirections private constructor() {
companion object {
fun viewDetail(): NavDirections =
ActionOnlyNavDirections(R.id.view_detail)
}
}
被包含的图接收到的参数似乎被忽略了--我无法安全地传递任何参数。
是否有解决方案?
我对任何能让我将图形分解成更小文件的替代方案都很感兴趣,不管是否基于<include />
。
2条答案
按热度按时间mspsb9vt1#
有一个解决方案:包括动作的参数,即
bxpogfeg2#
要解决此问题,您需要执行以下操作:
1.在
Included Graph
中,从<navigation>
块中删除item_id
<argument>
。1.在
<navigation>
块的Included Graph
中,将目的地添加到@id/itemDetailDragment
:1.在
Main Graph
中,确保所需的<action>
ID与Included Graph
中<action>
的ID相同,在您的情况下,它应该保持不变。Main Graph
中的<action>
id中删除+
符号,因为在这种情况下,它应该使用Included Graph
中的<action>
的id。*总的来说,你的代码应该是这样的: