saber pourrahimi
saber pourrahimi

Reputation: 105

Too many arguments for public final fun actionConformationFragmentToHomeFragment()

I want to send my arguments to another fragment using NavDirections

<fragment
    android:id="@+id/conformationFragment"
    android:name="com.example.panoramic.ui.ConformationFragment"
    android:label="ConformationFragment"
    tools:layout="@layout/fragment_conformation">
        <action
            android:id="@+id/action_conformationFragment_to_registerProductFragment"
            app:destination="@id/registerProductFragment" />
        <action
            android:id="@+id/action_conformationFragment_to_homeFragment"
            app:destination="@id/homeFragment" />
        <argument
            android:name="modelNumber"
            app:argType="string" />
        <argument
            android:name="serialNumber"
            app:argType="string" />
</fragment>

Here is how I attempt to send arguments:

class ConformationFragment : Fragment(R.layout.fragment_conformation) {

    private var fragmentConformationBinding: FragmentConformationBinding? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val binding = FragmentConformationBinding.bind(view)
        fragmentConformationBinding = binding
        val modelNumber = binding.modelValue.text.toString()
        val serialNumber = binding.serialValue.text.toString()
        binding.editingInformationButton.setOnClickListener {
            findNavController().navigate(R.id.action_conformationFragment_to_registerProductFragment)
        }
        binding.confirmButton.setOnClickListener {
            findNavController().navigate(ConformationFragmentDirections
                .actionConformationFragmentToHomeFragment(modelNumber, serialNumber))
        }

    }

    override fun onDestroyView() {
        fragmentConformationBinding = null
        super.onDestroyView()
    }
}

I am sure dependencies are correct, but still, the error appears:

Too many arguments for public final fun actionConformationFragmentToHomeFragment(): NavDirections defined in com.example.panoramic.ui.ConformationFragmentDirections.Companion
and red line under modelNumber and serialNumber in actionConformationFragmentToHomeFragment() function

I checked other similar question but none of them works for me.

Upvotes: 2

Views: 5972

Answers (1)

Jenea Vranceanu
Jenea Vranceanu

Reputation: 4694

The issue is that the arguments you defined in your navigation graph file are accepted by ConformationFragment and not by HomeFragment.

Arguments should be defined under <fragment> tag of a fragment that is accepting defined arguments, so every other navigation action to this fragment would accept arguments. This is how the navigation graph should be declared regarding ConformatioFragment and HomeFragment:

<fragment
    android:id="@+id/conformationFragment"
    android:name="com.example.panoramic.ui.ConformationFragment"
    android:label="ConformationFragment"
    tools:layout="@layout/fragment_conformation">
        <action
            android:id="@+id/action_conformationFragment_to_registerProductFragment"
            app:destination="@id/registerProductFragment" />
        <action
            android:id="@+id/action_conformationFragment_to_homeFragment"
            app:destination="@id/homeFragment" />
</fragment>

<fragment 
    android:id="@+id/homeFragment"
    android:name="com.example.panoramic.ui.HomeFragment"
    android:label="HomeFragment"
    tools:layout="@layout/fragment_home">
        <argument
            android:name="modelNumber"
            app:argType="string" />
        <argument
            android:name="serialNumber"
            app:argType="string" />
</fragment>

After these changes, you should be able to pass arguments into actionConformationFragmentToHomeFragment() function.

Upvotes: 5

Related Questions