Home > front end >  How can I send the array from the first fragment to the second fragment using Navigation Component?
How can I send the array from the first fragment to the second fragment using Navigation Component?

Time:03-04

FirstFragment.java

public class FirstFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        CategoryModel[] list = {new CategoryModel(1, "Category 1")
                , new CategoryModel(2, "Category 2")
                , new CategoryModel(3, "Category 3")
                , new CategoryModel(4, "Category 4")
                , new CategoryModel(5, "Category 5")};

        FirstFragmentDirections.actionFirstFragmentToSecondFragment(list);

        return inflater.inflate(R.layout.fragment_first, container, false);
    }

}

SecondFragment.java

public class SecondFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        CategoryModel[] list = SecondFragmentArgs.fromBundle(getArguments()).getCategories();

        return inflater.inflate(R.layout.fragment_second, container, false);
    }

}

CategoryModel.java

public class CategoryModel {

    private final int id;
    private final String name;

    public CategoryModel(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

}

main_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/main_navigation"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@ id/firstFragment"
        android:name="com.test.ttt.FirstFragment"
        tools:layout="@layout/fragment_first">

        <action
            android:id="@ id/action_firstFragment_to_secondFragment"
            app:destination="@id/secondFragment" />

    </fragment>

    <fragment
        android:id="@ id/secondFragment"
        android:name="com.test.ttt.SecondFragment"
        tools:layout="@layout/fragment_second">

        <argument
            android:name="categories"
            app:argType="com.test.ttt.CategoryModel[]" />

    </fragment>

</navigation>

I'm trying to send the categories from the first fragment to the second fragment but I got this error during run the app

enter image description here

I searched carefully before posting this question but I did not reach to any solution...............................

CodePudding user response:

You can pass bundle as a second param in navigate() method of NavController same as Intent.

Using Kotlin:

fun Activity.open(destination: Int, params: HashMap<String, Any?>) {
        try {
            val navController = findNavController(this, id of navigation graph container) // Mine is R.id.fragmentsContainer
            navController.navigate(destination, params.toBundle())
    
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

You can use above shortcut method for fragment navigation just pass destination fragment id & hashmap/replace it with bundle)

Using Java:

 void open(Integer destination, Bundle bundle) {
        try {
            NavController navController = Navigation.findNavController(this, id of navigation graph container); // Mine is R.id.fragmentsContainer
            navController.navigate(destination, bundle);

        } catch (e:Exception){
            e.printStackTrace()
        }
    }
  • Related