4

I am working on the project NavigationAdvancedSample of the architecture-components-samples repository. This project shows a workaround to support multiple back stacks for each tab of a BottomNavigationView.

In this configuration, you define a navigation graph for each of your tabs and let the class extension NavigationExtensions handle the different back stacks for you. Everything works fine, but I can't find a way to select the starting tab of the BottomNavigationView. I have tried to tweak the NavigationExtensions but without success.

By default, the selected tab on the application startup is the first of the bottom navigation view. How can I change this behavior in order to show the second or third tab for example?

Rafael Bugajewski
  • 1,702
  • 3
  • 22
  • 37

1 Answers1

0

Changing order of items in the navGraphIds and selecting item in bottomNavigationView helped me.

private fun setupBottomNavigationBar() {
    val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
    val navGraphIds = listOf(R.navigation.list, R.navigation.home, R.navigation.form)
    bottomNavigationView.selectedItemId = R.id.list
    // Setup the bottom navigation view with a list of navigation graphs
    val controller = bottomNavigationView.setupWithNavController(
        navGraphIds = navGraphIds,
        fragmentManager = supportFragmentManager,
        containerId = R.id.nav_host_container,
        intent = intent
    )
    // Whenever the selected controller changes, setup the action bar.
    controller.observe(this, Observer { navController ->
        setupActionBarWithNavController(navController)
    })
    currentNavController = controller
}

As a result I have R.id.list item as first.