当该参数具有默认值时,为什么不能使用导航组件将参数传递给fragment? [英] Why can't I pass an argument to fragment using navigation component when that argument has a default value?

查看:52
本文介绍了当该参数具有默认值时,为什么不能使用导航组件将参数传递给fragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用导航组件,但是我不明白为什么在定义了参数的情况下将参数传递给下面的方法时会出错.我使用的是SafeArgs,只有在为该参数定义默认值时才会出现此错误.

I am using navigation component and I don't understand why I get an error passing the argument to the method below if the argument is defined. I am using SafeArgs and I only get this error when I define a default value for this argument.

有人可以向我解释为什么会发生这种情况以及如何解决吗?

Could someone explain me why this happens and how can I solve it?

这是导航图代码的一部分.

Here is a part of the code of the navigation graph.

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

推荐答案

如文档中所述

目标级别的参数和默认值由导航到目标的所有操作使用.如果需要,您可以通过在操作级别定义一个参数来覆盖参数的默认值(如果不存在,则设置一个).此参数必须与目标中声明的参数具有相同的名称和类型.

Destination-level arguments and default values are used by all actions that navigate to the destination. If needed, you can override the default value of an argument (or set one if it doesn't already exist) by defining an argument at the action level. This argument must be of the same name and type as the argument declared in the destination.

所以您必须覆盖此值才能执行以下操作,因此您可以遵循以下两种解决方案中的任何一种方法

so you have to override this value to do so you can follow any of the both solutions below

解决方案一

 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>

然后您可以像这样调用导航

and then you can call your navigation like this

findNavController().navigate(SleepQualityFragmentDirections.actionSleepQualityFragmentToSleepTrackerFragment(5))

这篇关于当该参数具有默认值时,为什么不能使用导航组件将参数传递给fragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆