I am trying to replace a fragment with another fragment in my Bottom Navigation Activity. I want to achieve this on the click of a button in my fragment.
but I am getting the following error:
java.lang.IllegalArgumentException: No view found for id 0x7f090074 for fragment
here's my replace function:
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = childFragmentManager
fragmentManager.commit {
setReorderingAllowed(true)
replace(R.id.content, fragment)
}
}
XML code for Bottom Nav:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<fragment
android:id="@+id/home_nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/home_bottom_nav"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/home_nav" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/home_bottom_nav"
android:layout_width="match_parent"
android:layout_height="66dp"
android:background="#fff"
android:textAlignment="center"
app:itemIconTint="@drawable/home_bottom_nav_colour_selector"
app:itemTextColor="@drawable/home_bottom_nav_colour_selector"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
FirstFragment is to be replaced by SecondFragment and FirstFragment is currently displayed by the BottomNavigation
FirstFragment()
code:
class ExpenseFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button.setOnClickListener {
fragment = SecondFragment()
replaceFragment(fragment)
}
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = childFragmentManager
fragmentManager.commit {
setReorderingAllowed(true)
replace(R.id.content, fragment)
}
}
}
I've tried all the solutions to similar questions but my problem is still not solved