Remove up button from action bar when navigating using BottomNavigationView with Android Navigation UI library

jeffmcnd picture jeffmcnd · May 12, 2018 · Viewed 7.2k times · Source

I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        setSupportActionBar(toolbar)

        val navController = navHostFragment.findNavController()
        setupActionBarWithNavController(this, navController)
        setupWithNavController(bottomNav, navController)
    }

    override fun onSupportNavigateUp(): Boolean
            = findNavController(navHostFragment).navigateUp()

}

Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.

enter image description here

I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?

Answer

Javier Cancio picture Javier Cancio · Nov 25, 2018

Starting with 1.0.0-alpha07 you can use AppBarConfiguration to configure that behaviour.

AppBarConfiguration has a Builder constructor so you can create a new Builder with a specific set of top level destinations, referenced by their id (this id is the one you set on your navigation layout).

Create new AppBarConfiguration:

val appBarConfiguration = AppBarConfiguration
            .Builder(
                    R.id.navigationHomeFragment,
                    R.id.navigationListFragment,
                    R.id.navigationProfileFragment)
            .build()

Then, instead of setupActionBarWithNavController(this, navController) you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)

This is the right way to handle top navigation behaviours.