Search code examples
androidnavigationandroid-architecture-navigation

Navigating from click listener in a row to another fragment


I need to click the button in Fragment A to Navigate to Fragment C

am using Navigation Component , Data binding and Android ViewModel

row.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:padding="@dimen/padding">
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/img_edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin_small"
                android:layout_marginRight="@dimen/margin_small"
                app:srcCompat="@drawable/edit"
                app:tint="@color/grey"
                android:background="@drawable/circle"
                android:onClick="@{(v) -> handler.onEdit(v,var.accId)}"
                 />
    </RelativeLayout>
<data>
    <variable
        name="var"            type="Result" />
    <variable
        name="handler"            type="FragmentACallback" />
</data>
</layout>

FragmentA

    private void setListAdapter() {
    final FragmentACallback callback = new FragmentACallback() {
        @Override
        public void onEdit(View v, int accID) {
  /*
  i tried all these solutions but not working
   */

   // Navigation.createNavigateOnClickListener(R.id.action_nav).onClick(v);

    //                Navigation.findNavController(v.itemView).navigate(R.id.action_nav);
    //                NavHostFragment.findNavController(v).navigate(R.id.action_nav);
    Navigation.findNavController(getActivity(),R.id.nav_host_fragment).navigate(R.id.action_nav);
    //                Navigation.findNavController((MainActivity)(v.getContext()), R.id.nav_host_fragment).navigate(R.id.action_nav);
        }
    };

    adapter = new PageListAdapter(getActivity(), data, callback);
    binding.rcvList.setAdapter(adapter);
}

navigation.xml

<?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/mobile_navigation"
app:startDestination="@+id/nav_home"> 

<fragment
    android:id="@+id/nav_home"
    android:name=".HomeFragment"
    tools:layout="@layout/fragment_home" />

 <fragment
    android:id="@+id/nav_viewpager1"
    android:name=".ViewPager1"
    tools:layout="@layout/fragment_pager1">
    <action
        android:id="@+id/action_nav"
        app:destination="@id/fragment_pager2" />
</fragment>

 <fragment
    android:id="@+id/nav_viewpager2"
    android:name=".ViewPager2"
    tools:layout="@layout/fragment_pager2" />

<fragment
    android:id="@+id/nav_fragment_a"
    android:name=".FragmentA"
    tools:layout="@layout/fragment_a" />

<fragment
    android:id="@+id/nav_fragment_c"
    android:name=".FragmentC"
    tools:layout="@layout/fragment_C" />

</navigation>

i also tried to make the navigation from nav_fragment_a to nav_fragment_c but it always give me an exception

java.lang.IllegalArgumentException: navigation destination com.appname:id/action_nav is unknown to this NavController


Solution

  • To solve this issue make action as a global action as following:

    <?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/mobile_navigation"
    app:startDestination="@+id/nav_home"> 
    
    <fragment
    android:id="@+id/nav_home"
    android:name=".HomeFragment"
    tools:layout="@layout/fragment_home" />
    
     <fragment
    android:id="@+id/nav_viewpager1"
    android:name=".ViewPager1"
    tools:layout="@layout/fragment_pager1"/>
    
     <fragment
    android:id="@+id/nav_viewpager2"
    android:name=".ViewPager2"
    tools:layout="@layout/fragment_pager2" />
    
    <fragment
    android:id="@+id/nav_fragment_a"
    android:name=".FragmentA"
    tools:layout="@layout/fragment_a" />
    
    <fragment
    android:id="@+id/nav_fragment_c"
    android:name=".FragmentC"
    tools:layout="@layout/fragment_C" />
    
    <action
        android:id="@+id/action_nav"
        app:destination="@id/fragment_pager2" />
    </navigation>
    

    you can read more about global action in a navigation from here