1

While creating bottom navigation with the jetpack navigation component the code below would work for an activity

val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
findViewById<BottomNavigationView>(R.id.bottom_nav).setupWithNavController(navController)

with that, while working inside with fragments you need to call .setupWithNavController and pass in the navController and all should be fine.

But in my case with a navgraph inside the fragments XML and bottom navigation specified the app build but its only stuck in the home screen.

MainFragment.kt

class MainFragment : Fragment() {

    private var _binding: FragmentMainBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentMainBinding.inflate(inflater, container, false)
        NavigationUI.setupWithNavController(binding.bottomNav,findNavController())

        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.MainFragment">


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation_menu" />

    <fragment
        android:id="@+id/main_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/main_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

main_graph.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_gragh"
    app:startDestination="@id/homeFragment">
    <fragment
        android:id="@+id/homeFragment"
        android:name="io.github.jerrymatera.medstab.ui.home.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/appointmentFragment"
        android:name="io.github.jerrymatera.medstab.ui.appointment.AppointmentFragment"
        android:label="appointment_fragment"
        tools:layout="@layout/appointment_fragment" />
    <fragment
        android:id="@+id/chatFragment"
        android:name="io.github.jerrymatera.medstab.ui.chat.ChatFragment"
        android:label="chat_fragment"
        tools:layout="@layout/chat_fragment" />
    <fragment
        android:id="@+id/profileFragment"
        android:name="io.github.jerrymatera.medstab.ui.profile.ProfileFragment"
        android:label="profile_fragment"
        tools:layout="@layout/profile_fragment" />
</navigation>

menu/bottom_navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/mainFragment"
        android:icon="@drawable/ic_home_24"
        android:title="@string/home" />
    <item
        android:id="@+id/appointmentFragment"
        android:enabled="true"
        android:icon="@drawable/ic_date_range_24"
        android:title="@string/appointments" />
    <item
        android:id="@+id/chatFragment"
        android:enabled="true"
        android:icon="@drawable/ic_chat_24"
        android:title="@string/chat" />
    <item
        android:id="@+id/profileFragment"
        android:enabled="true"
        android:icon="@drawable/ic_account_box_24"
        android:title="@string/profile" />
</menu>

2 Answers 2

4

In menu resource of NavigationView (menu/bottom_navigation_menu.xml) each item ID must be matching with fragment ID in navigation graph(main_graph.xml). By this NavigationUI will figure out mapping between item and destination and it will perform fragment transaction on item selection.

change android:id="@+id/mainFragment" to android:id="@+id/homeFragment" in menu/bottom_navigation_menu.xml

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe the problem is in MainFragment.kt but I don't know the exact reason

NavigationUI.setupWithNavController(binding.bottomNav,findNavController())

require BottomNavigationView and NavController

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.