Dynamic ActionBar title from a Fragment using AndroidX Navigation

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

    Title can be changed in fragment using:

    ((AppCompatActivity) requireActivity()).getSupportActionBar().setTitle("Hello");
    
    0 讨论(0)
  • 2020-12-04 17:15

    Well, now the Navigation UI supports this feature. Now the ActionBar title changes dynamically. You just have to setup the ActionBar with the NavController.

    private lateinit var appBarConfiguration: AppBarConfiguration
    
    private lateinit var navController: NavController
    
    override fun onCreate(savedInstanceState: Bundle?) {
        preferedTheme()
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        navController = findNavController(R.id.nav_controller_fragment)
        appBarConfiguration = AppBarConfiguration(navController.graph)
        setupActionBarWithNavController(navController, appBarConfiguration)
    }
    

    And set action bar label in nav graph:

    <navigation xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/mobile_navigation"
            app:startDestination="@id/mainFragment">
    
    <fragment android:id="@+id/mainFragment"
              android:name="com.cinderellaman.general.ui.fragments.MainFragment"
              android:label="General"
              tools:layout="@layout/main_fragment"/>
    

    And now its also support Navigate Up:

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
    
    0 讨论(0)
  • 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:

      <fragment
        android:id="@+id/displayFragment"
        android:name="com.commonsware.jetpack.sampler.nav.DisplayFragment"
        android:label="Title: {title}" >
        <argument
          android:name="modelId"
          app:argType="string" />
        <argument
          android:name="title"
          app:argType="string" />
      </fragment>
    

    Here, the android:label attribute for our <fragment> 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.

    0 讨论(0)
  • 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"
    
                }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-04 17:19

    you can remove android:label in navigation graph then write in onCreateView()

    activity?.title="your title"
    
    0 讨论(0)
  • 2020-12-04 17:23

    Another solution is to use ViewModel and LiveData, attach viewmodel to your activity and fragments, add a livedata field inside viewmodel

    val title = MutableLiveData<String>()
    

    From your activity observe this field, and if it is changed update the toolbar title

    viewModel?.title?.observe(this, Observer { 
            my_toolbar.title=it
        })
    

    From your desired fragment change the title field inside the viewmodel

    viewModel?.title?.value="New title"
    
    0 讨论(0)
提交回复
热议问题