Dynamic ActionBar title from a Fragment using AndroidX Navigation

前端 未结 12 1708
别那么骄傲
别那么骄傲 2020-12-04 16:16

I am using the new Navigation component from Android Jetpack.

The root Activity setup is quite simple:

override fun onCreate(savedInstanceState: Bund         


        
12条回答
  •  时光说笑
    2020-12-04 17:17

    You can add addOnNavigatedListener inside your activity, and based on current destination change the title

     findNavController(nav_host_fragment).addOnNavigatedListener { controller, destination ->
            when(destination.id) {
                R.id.destination1 -> {
                    my_toolbar.title= "Some title"
                }
                R.id.destination2 -> {
                    my_toolbar.title= "Othertitle"
    
                }
    
        }
    }
    

提交回复
热议问题