Home > database >  Android BottomNavigation showes "Up Button" when navigating between tabs
Android BottomNavigation showes "Up Button" when navigating between tabs

Time:12-31

I've sat up a bottom navigation bar in my app. When I enable action bar with navController, the up button shows when navigating between tabs.

[Fig.1: up button is gone when I'm in home destination]

enter image description here


[Fig.2: up button show up when navigation between tabs]

enter image description here

It goes away when i remove this line in MainActivity but then I lose up button for the whole app:

setUpActionBarWithNavCotroller(navController)

How can i make "Up Button" disapper from bottom navigation tabs?

CodePudding user response:

I did a bit of furthur search and I found out that it's actualy possible to set multiple Top-level destinations manually so "Up Button" wouldn't be showed in them. Using AppBarConfiguration to add those destinations.

MainActivity

class MainActivity : AppCompatActivity() {

    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController

        binding.bottomNavigationView.setupWithNavController(navController)

        // Adding multiple top-level destinations using AppBarConfigutaion
        val appBarConfiguration = AppBarConfiguration(setOf(R.id.categoriesFragment, R.id.tasksFragment))
        setupActionBarWithNavController(navController, appBarConfiguration)
    }

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp() || super.onSupportNavigateUp()
    }
}

CodePudding user response:

Your Tasks and Categories are top-level destinations. So, you have to assign them to navController. Here's the official documentation.

Step 1:

Initialize AppBarConfiguration as follow

val appBarConfiguration = AppBarConfiguration(
        topLevelDestinationIds = setOf(R.id.tasksFragment, R.id.categoriesFragment)
    )

Step 2:

Assign them to navController with your Toolbar

findViewById<Toolbar>(R.id.toolbar)
        .setupWithNavController(navController, appBarConfiguration)

or ActionBar

setupActionBarWithNavController(navController, appBarConfiguration)
  • Related