0

I'm trying to make a navigation using NavigationUI as in this tutorial video. Below is code for my activity:

BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
NavController navController = navHostFragment.getNavController();

AppBarConfiguration appBarConfiguration = new AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
    R.id.dashboardMerchant, R.id.listMenuMerchant, R.id.addMenuMerchant).build();

NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(bottomNavigationView, navController);

My question is how to pass data from my activity to firstFragment or between framents. I already read this tutorial to pass bundle object, but I didn't know where to put the passing code in my activity or fragment.

Any idea how to do this?

Below is the XML file of my_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/merchant_navigation"
    app:startDestination="@id/listMenuMerchant">

    <fragment
        android:id="@+id/listMenuMerchant"
        android:name="com.example.fooddelivery.ListMenuMerchant"
        android:label="List Menu"
        tools:layout="@layout/fragment_list_menu">
        <argument
            android:name="user_info_string"
            app:argType="string"
            android:defaultValue=""/>
    </fragment>

    <fragment
        android:id="@+id/addMenuMerchant"
        android:name="com.example.fooddelivery.AddMenuMerchant"
        android:label="Add Menu"
        tools:layout="@layout/fragment_add_menu">
        <argument
            android:name="user_info_string"
            app:argType="string"
            android:defaultValue=""/>
    </fragment>
    <fragment
        android:id="@+id/dashboardMerchant"
        android:name="com.example.fooddelivery.DashboardMerchant"
        android:label="Dashboard"
        tools:layout="@layout/fragment_dashboard_merchant">
        <argument
            android:name="user_info_string"
            app:argType="string"
            android:defaultValue=""/>
    </fragment>
</navigation>
juliussin
  • 295
  • 3
  • 20
  • Can you share the navGraph XML file – Zain Jun 06 '21 at 22:20
  • @Zain I just added the XML code – juliussin Jun 06 '21 at 22:31
  • [Here](https://stackoverflow.com/questions/54596263/pass-data-from-single-activity-to-start-destination-fragment-using-architecture) to pass data from the `Activity` to the first fragment.. and [Here](https://stackoverflow.com/questions/59452243/android-navigation-component-pass-value-arguments-in-fragments) to pass data among fragments – Zain Jun 06 '21 at 22:45

0 Answers0