'How to Migrate to the Navigation library from FragmentManager
I want to make an app with a BottomNavigationView which has a single activity, which would contain a FragmentContainerView where all the fragments would be hosted.
I am trying to understand how Navigation works and how interoperable it is with FragmentManager. While following tutorials, I have ended up with the following piece of code in my supposed-to-be Main Activity.
private fun setCurrentFragment(fragment: Fragment){
supportFragmentManager.beginTransaction().apply {
replace(R.id.main_content,fragment)
commit()
}
}
Which is being used in a BottomNavigationView with a floating action button like so:
binding.fab.setOnClickListener {
setCurrentFragment(secondFragment)
}
And here is the code from activity_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=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:layout_constraintBottom_toTopOf="@id/appBar_layout"
app:layout_anchorGravity="center_vertical"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true">
</androidx.fragment.app.FragmentContainerView>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/appBar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:backgroundTint="#5175F6"
app:itemIconSize="26dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
app:elevation="0dp"
app:menu="@menu/bottom_nav_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/scan_again"
app:backgroundTint="#45B4F6"
app:layout_anchor="@id/bottom_bar"
app:maxImageSize="32dp"
app:srcCompat="@drawable/cam_icon"
tools:ignore="ImageContrastCheck"
tools:visibility="visible" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Now, I am trying to change this bottom navigation view to actually work with the Navigation library instead of doing the manual replacing and adding of fragments.
Currently, I'm trying to do the following within the MainActivity onCreate() method:
val navHostFragment = supportFragmentManager.findFragmentById(R.id.main_content) as NavHostFragment
navController = navHostFragment.navController
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation)
bottomNavigationView.setupWithNavController(navController)
and trying to use the navController within SecondFragment
in the following way:
val navHostFragment = parentFragmentManager.findFragmentById(R.id.main_content) as NavHostFragment
val navController = navHostFragment.navController
navController.navigate(R.id.modifyEvent,bundle)
The changing of fragments is possible within other Fragments such as SecondFragment in this way:
val modEvent = ModifyEvent()
modEvent.arguments = bundle
parentFragmentManager.beginTransaction().apply {
replace([email protected], modEvent)
commit()
}
But I want to make it work with the Navigation framework so I don't have to constantly use the FragmentManager with adding/replacing fragments.
Advice is appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
