Skip to content Skip to sidebar Skip to footer

Why Can't I Pass An Argument To Fragment Using Navigation Component When That Argument Has A Default Value?

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 th

Solution 1:

As mentioned in documentation

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

solution one

valaction=  SleepQualityFragmentDirections.actionSleepQualityFragmentToSleepTrackerFragment()
 action.qualityLastSleep = 5// your value
 findNavController().navigate(action)

solution two

<fragmentandroid:id="@+id/sleepTrackerFragment"android:name="com.example.sleeptracker.fragments.sleep_tracker.SleepTrackerFragment"android:label="fragment_sleep_tracker"tools:layout="@layout/fragment_sleep_tracker" ><argumentandroid:name="qualityLastSleep"app:argType="integer"android:defaultValue="-1" /><actionandroid:id="@+id/action_sleepTrackerFragment_to_sleepQualityFragment"app:destination="@id/sleepQualityFragment" /></fragment><fragmentandroid:id="@+id/sleepQualityFragment"android:name="com.example.sleeptracker.fragments.sleep_quality.SleepQualityFragment"android:label="fragment_sleep_quality"tools:layout="@layout/fragment_sleep_quality" ><actionandroid:id="@+id/action_sleepQualityFragment_to_sleepTrackerFragment"app:destination="@id/sleepTrackerFragment" ><argumentandroid:name="qualityLastSleep"app:argType="integer"android:defaultValue="-1" /></action></fragment>

and then you can call your navigation like this

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

Solution 2:

seems like what you really want is destination level argument:

Try the following:

<fragmentandroid:id="@+id/sleepTrackerFragment"android:name="com.example.sleeptracker.fragments.sleep_tracker.SleepTrackerFragment"android:label="fragment_sleep_tracker"tools:layout="@layout/fragment_sleep_tracker" ><actionandroid:id="@+id/action_sleepTrackerFragment_to_sleepQualityFragment"app:destination="@id/sleepQualityFragment" /></fragment><fragmentandroid:id="@+id/sleepQualityFragment"android:name="com.example.sleeptracker.fragments.sleep_quality.SleepQualityFragment"android:label="fragment_sleep_quality"tools:layout="@layout/fragment_sleep_quality" ><actionandroid:id="@+id/action_sleepQualityFragment_to_sleepTrackerFragment"app:destination="@id/sleepTrackerFragment" ><argumentandroid:name="qualityLastSleep"app:argType="integer"android:defaultValue="-1" /></action></fragment>

You can also Ctrl+Click on actionSleepQualityFragmentToSleepTrackerFragment() to check if the generated function accepts an argument.

Do try clean and rebuild if IDE complains about argument passing

Doc link: https://developer.android.com/guide/navigation/navigation-pass-data#override_a_destination_argument_in_an_action

Let me know if this does not work for you

Post a Comment for "Why Can't I Pass An Argument To Fragment Using Navigation Component When That Argument Has A Default Value?"