I am using bottom navigation view with navigation component. I used nested navhost in this app, the bottom navigation menu is selected but do not show their fragment. I follow some code but it doesn't work. I tried this code MainFragment.kt
class MainFragment : BaseFragment<FragmentMainBinding>(FragmentMainBinding::inflate) {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return super.onCreateView(inflater, container, savedInstanceState)
val navController = findNavController(fragmentBinding.fragment)
fragmentBinding.bottomNavigationView.setupWithNavController(navController)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
fragment_main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainFragment">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment"
android:name="com.newroz.myapplication.view.HomeFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:navGraph="@navigation/bottom_nav_graph"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
bottom_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/homeFragment"
android:title="Home"
android:icon="@drawable/ic_baseline_home_24"/>
<item
android:id="@+id/allUserFragment"
android:title="All User"
android:icon="@drawable/ic_baseline_people_24"/>
<item
android:id="@+id/profileFragment"
android:title="Profile"
android:icon="@drawable/ic_baseline_settings_24"/>
bottom_nav_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"
android:id="@+id/bottom_nav_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.newroz.myapplication.view.HomeFragment"
android:label="HomeFragment" />
<fragment
android:id="@+id/allUserFragment"
android:name="com.newroz.myapplication.view.AllUserFragment"
android:label="AllUserFragment" />
<fragment
android:id="@+id/profileFragment"
android:name="com.newroz.myapplication.view.ProfileFragment"
android:label="ProfileFragment" />
</navigation>
how can i show clicked fragment? Thank you