[英]How to pass a List as an argument to a Fragment using Navigation Component safeargs
[英]Why can't I pass an argument to fragment using navigation component when that argument has a default value?
我正在使用导航组件,但如果定义了参数,我不明白为什么将参数传递给下面的方法时会出错。 我正在使用 SafeArgs,只有在为此参数定义默认值时才会出现此错误。
有人可以解释一下为什么会发生这种情况,我该如何解决?
这是导航图的一部分代码。
<fragment
android:id="@+id/sleepTrackerFragment"
android:name="com.example.sleeptracker.fragments.sleep_tracker.SleepTrackerFragment"
android:label="fragment_sleep_tracker"
tools:layout="@layout/fragment_sleep_tracker" >
<action
android:id="@+id/action_sleepTrackerFragment_to_sleepQualityFragment"
app:destination="@id/sleepQualityFragment" />
<argument
android:name="qualityLastSleep"
app:argType="integer"
android:defaultValue="-1" />
</fragment>
<fragment
android:id="@+id/sleepQualityFragment"
android:name="com.example.sleeptracker.fragments.sleep_quality.SleepQualityFragment"
android:label="fragment_sleep_quality"
tools:layout="@layout/fragment_sleep_quality" >
<action
android:id="@+id/action_sleepQualityFragment_to_sleepTrackerFragment"
app:destination="@id/sleepTrackerFragment" >
</action>
</fragment>
似乎您真正想要的是目标级别的参数:
尝试以下操作:
<fragment
android:id="@+id/sleepTrackerFragment"
android:name="com.example.sleeptracker.fragments.sleep_tracker.SleepTrackerFragment"
android:label="fragment_sleep_tracker"
tools:layout="@layout/fragment_sleep_tracker" >
<action
android:id="@+id/action_sleepTrackerFragment_to_sleepQualityFragment"
app:destination="@id/sleepQualityFragment" />
</fragment>
<fragment
android:id="@+id/sleepQualityFragment"
android:name="com.example.sleeptracker.fragments.sleep_quality.SleepQualityFragment"
android:label="fragment_sleep_quality"
tools:layout="@layout/fragment_sleep_quality" >
<action
android:id="@+id/action_sleepQualityFragment_to_sleepTrackerFragment"
app:destination="@id/sleepTrackerFragment" >
<argument
android:name="qualityLastSleep"
app:argType="integer"
android:defaultValue="-1" />
</action>
</fragment>
您也可以 Ctrl+单击actionSleepQualityFragmentToSleepTrackerFragment()
以检查生成的 function 是否接受参数。
如果 IDE 抱怨参数传递,请尝试清理并重建
让我知道这是否不适合您
如文档中所述
目标级别 arguments 和默认值由导航到目标的所有操作使用。 如果需要,您可以通过在操作级别定义参数来覆盖参数的默认值(或设置一个,如果它不存在)。 此参数必须与目标中声明的参数具有相同的名称和类型。
因此您必须覆盖此值才能执行此操作,以便您可以遵循以下两种解决方案中的任何一种
解决方案一
val action = SleepQualityFragmentDirections.actionSleepQualityFragmentToSleepTrackerFragment()
action.qualityLastSleep = 5 // your value
findNavController().navigate(action)
解决方案二
<fragment
android:id="@+id/sleepTrackerFragment"
android:name="com.example.sleeptracker.fragments.sleep_tracker.SleepTrackerFragment"
android:label="fragment_sleep_tracker"
tools:layout="@layout/fragment_sleep_tracker" >
<argument
android:name="qualityLastSleep"
app:argType="integer"
android:defaultValue="-1" />
<action
android:id="@+id/action_sleepTrackerFragment_to_sleepQualityFragment"
app:destination="@id/sleepQualityFragment" />
</fragment>
<fragment
android:id="@+id/sleepQualityFragment"
android:name="com.example.sleeptracker.fragments.sleep_quality.SleepQualityFragment"
android:label="fragment_sleep_quality"
tools:layout="@layout/fragment_sleep_quality" >
<action
android:id="@+id/action_sleepQualityFragment_to_sleepTrackerFragment"
app:destination="@id/sleepTrackerFragment" >
<argument
android:name="qualityLastSleep"
app:argType="integer"
android:defaultValue="-1" />
</action>
</fragment>
然后你可以像这样调用你的导航
findNavController().navigate(SleepQualityFragmentDirections.actionSleepQualityFragmentToSleepTrackerFragment(5))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.