skip to Main Content

I am new to navigation graphs and navigation components in general and I am trying to pass data from the recycler view that’s located in ‘PrivSectionFragment’ to ‘PrivSectionDetailFragment’. Unfortunately, whenever I run the app I receive this error in my log

 java.lang.IllegalArgumentException: Navigation action/destination com.gazelle.wadifatk:id/action_privSectionFragment_to_privsDetailFragment cannot be found from the current destination Destination(com.gazelle.wadifatk:id/jobsFragment) label=الوظائف class=com.gazelle.wadifatk.jobLists.jobsFragment
    at androidx.navigation.NavController.navigate(NavController.java:940)
    at androidx.navigation.NavController.navigate(NavController.java:877)
    at androidx.navigation.NavController.navigate(NavController.java:863)
    at androidx.navigation.NavController.navigate(NavController.java:1159)
    at com.gazelle.wadifatk.jobLists.PrivListAdapter.onBindViewHolder$lambda-0(PrivListAdapter.kt:55)

This is where it refers // it refers to my recycler adapter

PushDownAnim.setPushDownAnimTo(holder.card_view)
        //.setScale(PushDownAnim.MODE_SCALE, 0.89f)
        .setOnClickListener {
            val sendData = PrivSectionFragmentDirections
                .actionPrivSectionFragmentToPrivsDetailFragment(priv_list[position])
            Navigation.findNavController(view).navigate(sendData)
        }

This is my_nav.xml // the blue circle
my_nav photo

keep in mind that I was able to pass the data from ‘LatestNewsFragment’ to ‘NewsDetailsFragment’
just by using the same moves and I don’t know why it accepted the data passing between those fragments and didn’t accept it from ‘PrivSectionFragment’ to ‘PrivSectionDetailFragment’.

here is the code of the fragments

<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/my_nav"
app:startDestination="@id/latestNewsFragment">

<fragment
    android:id="@+id/latestNewsFragment"
    android:name="com.gazelle.wadifatk.newsList.LatestNewsFragment"
    android:label="اخر الاخبار"
    tools:layout="@layout/fragment_latest_news" >
    <action
        android:id="@+id/action_latestNewsFragment_to_jobsFragment"
        app:destination="@id/jobsFragment"
        app:enterAnim="@anim/nav_default_enter_anim"
        app:exitAnim="@anim/nav_default_exit_anim" />
    <action
        android:id="@+id/action_latestNewsFragment_to_newsDetailFragment"
        app:destination="@id/newsDetailFragment"
        app:enterAnim="@android:anim/slide_in_left"
        app:exitAnim="@android:anim/slide_out_right"
        app:popEnterAnim="@android:anim/slide_in_left"
        app:popExitAnim="@android:anim/slide_out_right" />
</fragment>
<fragment
    android:id="@+id/jobsFragment"
    android:name="com.gazelle.wadifatk.jobLists.jobsFragment"
    android:label="الوظائف"
    tools:layout="@layout/fragment_jobs" >
    <action
        android:id="@+id/action_jobsFragment_to_govSectionFragment"
        app:destination="@id/govSectionFragment" />
    <action
        android:id="@+id/action_jobsFragment_to_privSectionFragment2"
        app:destination="@id/privSectionFragment" />
    <action
        android:id="@+id/action_jobsFragment_to_latestNewsFragment"
        app:destination="@id/latestNewsFragment" />
</fragment>
<fragment
    android:id="@+id/privSectionFragment"
    android:name="com.gazelle.wadifatk.jobLists.PrivSectionFragment"
    android:label="PrivSectionFragment" >
    <action
        android:id="@+id/action_privSectionFragment_to_privsDetailFragment"
        app:destination="@id/privsDetailFragment"
        app:enterAnim="@anim/nav_default_enter_anim"
        app:exitAnim="@anim/nav_default_exit_anim" />
    <action
        android:id="@+id/action_privSectionFragment_to_jobsFragment"
        app:destination="@id/jobsFragment" />
</fragment>
<fragment
    android:id="@+id/newsDetailFragment"
    android:name="com.gazelle.wadifatk.newsList.NewsDetailFragment"
    android:label="التفاصيل"
    tools:layout="@layout/fragment_news_detail">
    <action
        android:id="@+id/action_newsDetailFragment_to_latestNewsFragment"
        app:destination="@id/latestNewsFragment"
        app:enterAnim="@android:anim/slide_in_left"
        app:exitAnim="@android:anim/slide_out_right"
        app:popEnterAnim="@android:anim/slide_in_left"
        app:popExitAnim="@android:anim/slide_out_right" />
    <argument
        android:name="news_data_list"
        app:argType="com.gazelle.wadifatk.newsList.NewsListViewModel" />

</fragment>

 <fragment
    android:id="@+id/privsDetailFragment"
    android:name="com.gazelle.wadifatk.jobLists.PrivsDetailFragment"
    android:label="التفاصيل"
    tools:layout="@layout/fragment_privs_detail" >

    <argument
        android:name="priv_data_list"
        app:argType="com.gazelle.wadifatk.jobLists.PrivListViewModel" />
    <action
        android:id="@+id/action_privsDetailFragment_to_privSectionFragment"
        app:destination="@id/privSectionFragment" />

</fragment>

Here is a screen shot of the jobsFragment()

enter image description here

2

Answers


  1. You need to add only JobsFragment in your nav graph and add action in this fragment for PrivsDetailFragment.

    <fragment
        android:id="@+id/jobsFragment"
        android:name="com.gazelle.wadifatk.jobLists.jobsFragment"
        android:label="الوظائف"
        tools:layout="@layout/fragment_jobs" >
        <action
            android:id="@+id/action_jobsFragment_to_govSectionFragment"
            app:destination="@id/govSectionFragment" />
        <action
            android:id="@+id/action_jobsFragment_to_latestNewsFragment"
            app:destination="@id/latestNewsFragment" />
        <action
            android:id="@+id/action_jobsFragment_to_privsDetailFragment"
            app:destination="@id/privsDetailFragment"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim" />
    </fragment>
    

    And then in you adapter, using this action:

    PushDownAnim.setPushDownAnimTo(holder.card_view)
            //.setScale(PushDownAnim.MODE_SCALE, 0.89f)
            .setOnClickListener {
                val sendData = JobFragmentDirections.actionJobFragmentToPrivsDetailFragment(priv_list[position])
                Navigation.findNavController(view).navigate(sendData)
            }
    

    You can remove privSectionFragment from the nav graph.

    Login or Signup to reply.
  2. Verify if your destination needs arguments, if it’s true, You could add in your action

        PushDownAnim.setPushDownAnimTo(holder.card_view)
            //.setScale(PushDownAnim.MODE_SCALE, 0.89f)
            .setOnClickListener {
                val sendData = PrivSectionFragmentDirections
    
        sendData.setNameExtra(valueExtra)
                    .actionPrivSectionFragmentToPrivsDetailFragment(priv_list[position])
                Navigation.findNavController(view).navigate(sendData)
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search