How to remove up navigation from a specific fragment while using Android Navigation Component Library.

Florence Njeri
1 min readAug 17, 2020

--

Before:

/**
* Connect the navigation controller to the NavHostFragment
*/
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController

NavigationUI.setupActionBarWithNavController(this, navController)
Book Fragment having the up navigation button.

To remove the up navigation button, create an AppBarConfiguration of all the fragments you don't want to contain the up button for navigation.

/**
* Connect the navigation controller to the NavHostFragment
*/
val appBarConfiguration = AppBarConfiguration
.Builder(
R.id.BooksFragment,
R.id.logInFragment
)
.build()
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController

NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
Book Fragment after removing the up navigation button.

I have now removed the up action button from both the BooksFragment and the LogInFragment.

Happy coding!

--

--

Florence Njeri
Florence Njeri

Written by Florence Njeri

Software Engineering || Career || Life

Responses (1)