Search code examples
androidnavigationbottomnavigationviewandroid-jetpackandroid-jetpack-navigation

Android Jetpack Navigation navController problems


Cant understanding why it's not working... another analog project bnv working correctly... in-app app:startDestination="@+id/navigate_hot" work correctly, but on click bnv items - nothing happens

mainActivity.XML

  <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bttm_nav"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white"
        android:elevation="4dp"
        app:itemIconSize="35dp"
        app:itemTextColor="@color/bnv_tab_item_foreground"
        app:itemIconTint="@color/bnv_tab_item_foreground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/bottom_nav_menu"/>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


            <fragment
                android:id="@+id/nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:navGraph="@navigation/bnv_navigation" /> 

MainActivity.kt

  override fun onSupportNavigateUp(): Boolean {
        return findNavController(this@ActivityMain,R. id.nav_host_fragment).navigateUp()
    }

    private fun setupNavigation() {
        val navController = findNavController(this@ActivityMain, R.id.nav_host_fragment)
        bttm_nav.setupWithNavController(navController)
    } 

bnv_navigation.XML

<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/nav_graph"
    app:startDestination="@+id/navigate_hot">

    <fragment
        android:id="@+id/navigate_news"
        android:name="com.example.fatsale.fragments.NewsFragment"
        android:label="News"
        tools:layout="@layout/fragment_news" />
    <fragment
        android:id="@+id/navigate_hot"
        android:name="com.example.fatsale.fragments.HotFragment"
        android:label="Collection"
        tools:layout="@layout/fragment_hot" />
    <fragment
        android:id="@+id/navigate_profile"
        android:name="com.example.fatsale.fragments.ProfileFragment"
        android:label="Profile"
        tools:layout="@layout/fragment_profile" />

</navigation>

tell this dunkey what need to do..


Solution

  • In your menu file for bottomnavigationview, each item id has to same as fragment id which is added in navigation graph xml file. Also in menu file, Each item has to give OrderInCategory for fragment position no in bottomnavigationview.