Dynamic ActionBar title from a Fragment using AndroidX Navigation

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

    As of now, The Jetpack Navigation Architecture components do not provide any "built in" way to do this, and you'll have to implement your own "custom" method for doing it.

    There is an existing feature request to get functionality for dynamic labels on destinations added to the new Jetpack Navigation Architecture Components. If you are here because you want/need this functionality, please star the existing feature request, here: https://issuetracker.google.com/issues/80267266

    0 讨论(0)
  • 2020-12-04 17:07

    Remove label from graph.xml file

    android:label="fragment_info"
    

    and use old school approach if you want to set title of the fragment dynamically from the fragment itself

    getActivity().setTitle("Your Title");
    
    0 讨论(0)
  • 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")
    
    0 讨论(0)
  • 2020-12-04 17:12

    On trying the activity's title it seems to override the title for fragment. Being on safe side you must put on onResume.

    override fun onResume() {
        super.onResume()
        activity?.toolbar.title = "YOUR_TITLE_HERE"
    }
    

    it works for me !

    Note : Must have Toolbar Widget in activity

    Add toolbar like this in your activity's xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"">
    
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </com.google.android.material.appbar.AppBarLayout>
    
        <!-- Other Widgets -->
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
    0 讨论(0)
  • 2020-12-04 17:14

    Taking consideration that your host activity is MainActivity, just add the following code to your MainActivity's onCreate fun

    val navController = Navigation.findNavController(this, R.id.nav_host_fragment)
    
    // setting title according to fragment
    navController.addOnDestinationChangedListener { 
        controller, destination, arguments ->
            toolbar.title = navController.currentDestination?.label
    }
    
    0 讨论(0)
  • 2020-12-04 17:14

    If you use toolbar on setSupportActionBar in Activity and you would like to change its title in fragment, then below code may gonna help you ;)

    (requireActivity() as MainActivity).toolbar.title = "Title here"
    
    0 讨论(0)
提交回复
热议问题