Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 989
jeffmcnd Avatar asked May 12 '18 00:05

jeffmcnd


People also ask

What is NavController in Android?

NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.

How do I hide the bottom of my navigation?

Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.

What is onSupportNavigateUp?

onSupportNavigateUp() This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar. ActionMode. onWindowStartingSupportActionMode(ActionMode.Callback callback) Called when a support action mode is being started for this window.


1 Answers

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.

like image 165
Javier Cancio Avatar answered Sep 22 '22 11:09

Javier Cancio