ebeninki
ebeninki

Reputation: 989

Toolbar and ActionBar with NavigationUI

I have a Toolbar in my XML layout of the main activity. When I create a Toolbar with the NavigationUI as described at https://developer.android.com/guide/navigation/navigation-ui , then is it also too much when I create an ActionBar with the NavigationUI? So, in my main activity I have the following:

override fun onCreate(savedInstanceState: Bundle?) {
     // AppBar configuration
     appBarConfiguration =AppBarConfiguration.Builder(setOf(R.id.homeFragment)).build()
     // NavController object
     val navController: NavController = findNavController(R.id.myNavHostFragment)
     // create Toolbar using NavigationUI method
     setupToolbar(navController, appBarConfiguration)

     // create ActionBar using NavigationUI method
     setupActionBar(navController, appBarConfiguration)
}

private fun setupToolbar(navController: NavController, appBarConfiguration: AppBarConfiguration) {
     binding.toolbar.setupWithNavController(navController, appBarConfiguration)
}

private fun setupActionBar(navController: NavController, appBarConfiguration: AppBarConfiguration) {
     setupActionBarWithNavController(navController, appBarConfiguration)
}

I am a little bit confused. Do I need both of them ? Do I need only the setupToolbar() ? What is the difference between them ?

Hope someone can help.

Upvotes: 1

Views: 464

Answers (1)

Rarity7-
Rarity7-

Reputation: 187

It depends on what you use. If you use a <toolbar> in XML then you have to use only setupToolbar. But if you use default ActionBar, which is provided by Theme.AppCompat.Light in style.xml then you have to use setupActionBar().

It seems that you use default ActionBar. Try to use only setupToolabr() method.

Upvotes: 2

Related Questions