Search code examples
androidkotlinbottomnavigationview

BottomNavigation GONE


I have a bottom navigation that is being created in my Main Activity, however, I want to make it disappear in a fragment. How can I make my bottom navigation disappear?

Class Main Activity

class MainActivity : AppCompatActivity() {
    private val controlador by lazy {
        findNavController(R.id.pokemons_activity_nav_host)
    }

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
    configNavControler()

}

private fun configNavControler() {
    val navView: BottomNavigationView = findViewById(R.id.main_activity_bottom_navigation)
    val navHostFragment = supportFragmentManager
        .findFragmentById(R.id.pokemons_activity_nav_host) as NavHostFragment
    val navController = navHostFragment.navController

    navView.setupWithNavController(navController)
  }
}

Fragment class where I want my bottom navigation not to appear

class EntradaFragment : Fragment() {
private val controlador by lazy {
    findNavController()
}
override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(
        R.layout.entrada,
        container,
        false
    )

}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    botao_entrar.setOnClickListener {
        val direcao = EntradaFragmentDirections.acaoEntradaParaListaPokemons()
        controlador.navigate(direcao)
    }
  }
}

Solution

  • The topDestinationIds property contains ids of your main fragments. When the navigation is changed, the nav controller's listener is called, there you change the visibility of your bottom navigation view.

    class MainActivity : AppCompatActivity() {
        private val controlador by lazy {
            findNavController(R.id.pokemons_activity_nav_host)
        }
    
        private val topDestinationIds = setOf(
            R.id.firstFragment,
            R.id.secondFragment,
            R.id.thirdFragment
        )
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        configNavControler()
    
    }
    
    private fun configNavControler() {
        val navView: BottomNavigationView = findViewById(R.id.main_activity_bottom_navigation)
        val navHostFragment = supportFragmentManager
            .findFragmentById(R.id.pokemons_activity_nav_host) as NavHostFragment
        val navController = navHostFragment.navController
    
        navView.setupWithNavController(navController)
    
        navController.addOnDestinationChangedListener { _, destination, _ ->
            if (destination.id !in topDestinationIds) {
                navView.visibility = View.GONE
            } else {
                navView.visibility = View.VISIBLE
            }
        }
    
      }
    }