CodexBloom - Programming Q&A Platform

how to get Navigation Component to work with BottomNavigationView and Fragments in Android 14

πŸ‘€ Views: 329 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-08
android navigation-component bottomnavigationview fragments kotlin

I'm getting frustrated with I'm not sure how to approach I've looked through the documentation and I'm still confused about I've hit a wall trying to I tried several approaches but none seem to work... I've been banging my head against this for hours. I'm working with an scenario with integrating the Navigation Component with a BottomNavigationView in my Android 14 application. I have a BottomNavigationView that is supposed to switch between three fragments: HomeFragment, DashboardFragment, and NotificationsFragment. However, when I click on the navigation items, the fragments do not change as expected, and I see the following behavior in the logcat: ``` java.lang.IllegalStateException: Fragment already added: HomeFragment ``` I've set up my navigation graph in XML, and I'm using the following code in my MainActivity: ```kotlin class MainActivity : AppCompatActivity() { private lateinit var navController: NavController override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment navController = navHostFragment.navController val bottomNavView = findViewById<BottomNavigationView>(R.id.bottom_navigation) bottomNavView.setupWithNavController(navController) } } ``` My navigation graph is defined in `res/navigation/nav_graph.xml`, structured as follows: ```xml <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" app:startDestination="homeFragment"> <fragment android:id="@+id/homeFragment" android:name="com.example.app.HomeFragment" android:label="Home" tools:layout="fragment_home"> </fragment> <fragment android:id="@+id/dashboardFragment" android:name="com.example.app.DashboardFragment" android:label="Dashboard" tools:layout="fragment_dashboard"> </fragment> <fragment android:id="@+id/notificationsFragment" android:name="com.example.app.NotificationsFragment" android:label="Notifications" tools:layout="fragment_notifications"> </fragment> </navigation> ``` I've also tried resetting the BottomNavigationView's item selection when a fragment is switched, but it doesn't seem to resolve the scenario. Here’s how I attempted to handle the navigation item selections: ```kotlin bottomNavView.setOnNavigationItemSelectedListener { item -> when (item.itemId) { R.id.navigation_home -> { navController.navigate(R.id.homeFragment) true } R.id.navigation_dashboard -> { navController.navigate(R.id.dashboardFragment) true } R.id.navigation_notifications -> { navController.navigate(R.id.notificationsFragment) true } else -> false } } ``` Despite following the documentation, the fragments are not swapping correctly, and I keep getting the same exception. Could anyone guide to figure out what might be going wrong here? My development environment is Ubuntu. This is part of a larger web app I'm building. Is there a better approach? I've been using Kotlin for about a year now. Could someone point me to the right documentation? I'm using Kotlin stable in this project. Any help would be greatly appreciated!