I am using the new Navigation component from Android Jetpack.
The root Activity setup is quite simple:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val navController = findNavController(R.id.navigationFragment)
setupActionBarWithNavController(navController)
bottomNavigationView.setupWithNavController(navController)
}
It works well when the Fragment's title is defined in the navigation graph. But for one Fragment, I want to set the title dynamically.
I tried with findNavController().currentDestination.label = "Hello world"
but it does nothing.
I could of course use a trick like (activity as? AppCompatActivity)?.supportActionBar?.title = "Hello world"
, but I feel that it will break the magic that setupActionBarWithNavController()
does for me. It there any way to update the Action Bar title dynamically?