Home > Back-end >  Bottom Navigation did not navigate when item is clicked
Bottom Navigation did not navigate when item is clicked

Time:06-25

I created two activities as a navcontroller for the fragment, I am not sure if this is the right approach.

For MyActivity, the bottom navigation is working well and showing the fragment. But for the MainActivityRelawan the Home Menu didn't navigate when item is clicked and the fragment only is shown after I press back button, I don't know why.

The main_activity is for controlling the Main Activity Fragment, and main_activity_relawan is for controlling Main Activity Relawan Fragment.

This is my MainActivityRelawan

 val navController = findNavController(R.id.nav_host_fragment_activity_main_relawan)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home_relawan, R.id.navigation_dashboard, R.id.navigation_notifications
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

And this is my MainActivity

val navController = findNavController(R.id.nav_host_fragment_activity_main)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

and for each of findNavController i use different activity XML and different navGraph this is my MainActivity findNavController

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@ id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@ id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:navGraph="@navigation/mobile_navigation" />

and this is my MainActivityRelawan NavController

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@ id/nav_view_relawan"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@ id/nav_host_fragment_activity_main_relawan"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view_relawan"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation_relawan" />

and i using different navGraph for each. This is NavigationGraph for MainActivity

 <fragment
        android:id="@ id/navigation_home"
        android:name="com.example.tes.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@ id/navigation_dashboard"
        android:name="com.example.tes.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@ id/navigation_notifications"
        android:name="com.example.tes.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />

and this is NavGraph for the MainActivityRelawan

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


    <fragment
        android:id="@ id/navigation_home_relawan"
        android:name="com.example.tes.ui.home_relawan.RelawanFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_main_relawan" >
        <action
            android:id="@ id/action_navigation_home_relawan_to_navigation_dashboard"
            app:destination="@id/navigation_dashboard" />
    </fragment>

    <fragment
        android:id="@ id/navigation_dashboard"
        android:name="com.example.tes.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" >
        <action
            android:id="@ id/action_navigation_dashboard_to_navigation_notifications"
            app:destination="@id/navigation_notifications" />
    </fragment>

    <fragment
        android:id="@ id/navigation_notifications"
        android:name="com.example.tes.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" >

    </fragment>



</navigation>

CodePudding user response:

I used it in one Activity. I doun't know it's good or not. So you can see solution in my git

CodePudding user response:

solved, this is happened just because i made different id for the menu bottom navigation

  • Related