1

I am trying to implement a bottom navigation with using Jetpack Navigation. In addition to fragments in the bottom navigation, there is another fragment which I designed it to be the starting destination.

The problem is that when I start the app, my starting destination (Fragment Play) is displayed on the screen but the item in bottom navigation starts as checked (Scoreboard) which I don't want it to be checked at the start.

Starting destination

My MainActivity codes;

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration

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

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

    navController = navHostFragment.findNavController()
    appBarConfiguration = AppBarConfiguration(
        setOf(R.id.scoreboardFragment, R.id.playFragment, R.id.settingsFragment)
    )

    binding.apply {
        setSupportActionBar(toolbar)
        setupActionBarWithNavController(navController, appBarConfiguration)

        bottomNavigation.setupWithNavController(navController)
        bottomNavigation.background = null
    }

}

I tried to use bottomNavigation.menu[0].isChecked = false and bottomNavigation.menu.setGroupCheckable(0, false, true) from the question but setGroupCheckable() also prevents the tint color on selected item and setting isChecked = false didn't do anything or I them it wrong.

So is there any other way to fix this from removing the starting destination from the FAB and adding it to the bottom navigation as an additional item? Because this design is more convenient and cool for my app in my opinion.

Thanks in advance.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141

1 Answers1

0

The answer is late but this will help others like me. The same situation is required in my project. I have achieved this by the following code

  binding.bottomNavigation.menu.setGroupCheckable(0, false, true)

  binding.bottomNavigation.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.scoreboardFragment-> {
                    navController.navigate(R.id.scoreboardFragment)
                    item.isCheckable = true
                    return@setOnItemSelectedListener true
                }
                R.id.settingsFragment-> {
                    navController.navigate(R.id.settingsFragment)
                    item.isCheckable = true
                    return@setOnItemSelectedListener true
                }
            }
            false
        }

 binding.fabPlayButton.setOnClickListener{
            if (navController.currentDestination?.id != R.id.playFragment){
                navController.navigate(R.id.playFragment)
                binding.bottomNavigation.menu.setGroupCheckable(0, false, true)
            }
        }

Note:

In this case, you should navigate manually by id

Yaqoob Bhatti
  • 1,271
  • 3
  • 14
  • 30