Search code examples
androidandroid-architecture-navigationandroid-jetpack-navigation

toolbar title reset by Activity when using androidx.navigation


using androidx.navigation the toolbar-title ist set automatically when using android.label in the nav_graph. this works fine until a configuration-change (e.g. rotate the device) happens. after this, the toolbar-title is reset to the app-title.

it seems that the activity is re-setting the title in Activity.onPostCreate() after navigation's ToolbarOnDestinationChangedListener has set it after the rotation.


Solution

  • You have to set up toolbar both with navigation graph and as a support action bar.

    val appCompat = requireActivity() as AppCompatActivity
    appCompat.setSupportActionBar(toolbar)
    appCompat.setupActionBarWithNavController(navController)
    

    Check this for more information.