Dynamic ActionBar title from a Fragment using AndroidX Navigation

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

    As of 1.0.0-alpha08, you can have the NavigationUI bits dynamically set the title... if the dynamic bits are arguments on the navigation action.

    So, for example, in your navigation graph, you could have something like this:

      
        
        
      
    

    Here, the android:label attribute for our has an argument name wrapped in braces ({title} in "Title: {title}". The app bar's title will then be set to the value of the label, with {title} replaced by the value of the title argument.

    If you need something more elaborate than that — for example, you want to look up the model by ID and read a property from it — you will need to use more manual approaches, such as those outlined in other answers to this question.

提交回复
热议问题