Dynamic ActionBar title from a Fragment using AndroidX Navigation

前端 未结 12 1707
别那么骄傲
别那么骄傲 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:08

    Until the issue will be fixed, simple listener is working to me:

    /**
     * Temporary solution to dynamically change title of actionbar controlled by Navigation component
     * Should be removed as soon as the bug on Navigation will be fixed: (https://issuetracker.google.com/issues/80267266)
     */
    interface TempToolbarTitleListener {
        fun updateTitle(title: String)
    }
    
    class MainActivity : AppCompatActivity(), TempToolbarTitleListener {
    
        ...
    
        override fun updateTitle(title: String) {
            binding.toolbar.title = title
        }
    }
    

    change title from fragment:

    (activity as TempToolbarTitleListener).updateTitle("custom title")
    

提交回复
热议问题