Search code examples
androidkotlinandroid-jetpack-navigation

FragmentArgs class in not generated while passing value of one fragment to another by using Navigation components


This is my FragmentTwo fragment class code.

class FragmentTwo : Fragment() {

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?,
    ): View? {
        // Inflate the layout for this fragment
        val binding : FragmentTwoBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_two, container, false)
        var args = FragmentTwoArgs.fromBundle(arguments)
        setHasOptionsMenu(true)
        return binding.root
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        inflater?.inflate(R.menu.overflow_menu,menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return NavigationUI.onNavDestinationSelected(item!!,findNavController())
                || super.onOptionsItemSelected(item)
    }
}

This is my FragmentOne fragment class code:

class FragmentOne : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        // return inflater.inflate(R.layout.fragment_one, container, false)
        val binding: FragmentOneBinding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_one, container, false)
        binding.clickable = this
        return binding.root
    }

    fun onClicking() {
        //Toast.makeText(activity, "You clicked me.", Toast.LENGTH_SHORT).show()
        //findNavController().navigate(R.id.action_fragmentOne_to_fragmentTwo)

        findNavController().navigate(FragmentOneDirections.actionFragmentOneToFragmentTwo())


    }

}

And this is my Navigation xml code.

<?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"
    android:id="@+id/navigation"
    app:startDestination="@id/fragmentOne">

    <fragment
        android:id="@+id/fragmentOne"
        android:name="com.example.fragmentpractise1.FragmentOne"
        android:label="fragment_one"
        tools:layout="@layout/fragment_one" >
        <action
            android:id="@+id/action_fragmentOne_to_fragmentTwo"
            app:destination="@id/fragmentTwo" />
        <argument
            android:name="numViews"
            app:argType="integer"
            android:defaultValue="18" />
    </fragment>
    <fragment
        android:id="@+id/fragmentTwo"
        android:name="com.example.fragmentpractise1.FragmentTwo"
        android:label="fragment_two"
        tools:layout="@layout/fragment_two" />
    <fragment
        android:id="@+id/aboutFragment"
        android:name="com.example.fragmentpractise1.AboutFragment"
        android:label="fragment_about"
        tools:layout="@layout/fragment_about" />
</navigation>

Now I am getting an error in FragmentTwo class code as FragmentTwoArgs class is not generated while assigning it to args variable. I am using Nav safe args and defined argument value through Nav graph in FragmentOne. Any help would be appreciated.


Solution

  • You're using wrong Fragment to declare the arguments. If you want FragmentTwo to have arguments you should use the fragment in the navigation xml:

    <fragment
        android:id="@+id/fragmentTwo"
        android:name="com.example.fragmentpractise1.FragmentTwo"
        android:label="fragment_two"
        tools:layout="@layout/fragment_two">
    
        <argument
            android:name="numViews"
            app:argType="integer"
            android:defaultValue="18" />
    </fragment>
    

    You might also want to use the lazy delegate navArgs():

    private val args: FragmentTwoArgs by navArgs()