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

Android Navigation Component not displaying correct action bar title


I have a MainActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)
        val navController = findNavController(R.id.nav_host_fragment)

        toolbar.setupWithNavController(navController, AppBarConfiguration(navController.graph))
    }
}

And 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/nav_graph_main.xml"
    app:startDestination="@id/AFragment">

    <fragment
        android:id="@+id/AFragment"
        android:name="com.example.actionbartitletest.AFragment"
        android:label="Fragment A"
        tools:layout="@layout/fragment_a" >
        <action
            android:id="@+id/action_AFragment_to_BFragment"
            app:destination="@id/BFragment" />
    </fragment>
    <fragment
        android:id="@+id/BFragment"
        android:name="com.example.actionbartitletest.BFragment"
        android:label="Fragment B"
        tools:layout="@layout/fragment_b" />
</navigation>

And Manifest.xml

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

And strings.xml

<resources>
    <string name="app_name">ActionBarTitleTest</string>
    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hello blank fragment</string>
</resources>

When the app launches the action bar title is ActionBarTitleTest (presumably the application android:label defined in the Manifest.xml), however I'm expecting the action bar title to be Fragment A as it's defined in the android:label attribute of the nav graph. When I navigate from AFragment -> BFragment the action bar title on BFragment is Fragment B as expected and when navigating back from BFragment -> AFragment the action bar title then reads Fragment A as expected.

Why is the action bar title getting set to the application android:label when the application launches and not the android:label defined in the nav graph? I realize I can manage this at runtime by calling requireActivity().setTitle() in the fragments, but I feel like using android:label from the nav graph should be sufficient.

AFragment at app launch Navigate to BFragment

Navigate back to AFragment


Solution

  • You are using the setSupportActionBar(toolbar) method.

    In this case you should add navigation support to the default action bar using:

    setupActionBarWithNavController(navController, AppBarConfiguration(navController.graph))
    

    instead of

    toolbar.setupWithNavController(navController, AppBarConfiguration(navController.graph))
    

    Something like

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)
    
        val navController = findNavController(R.id.nav_host_fragment)
        setupActionBarWithNavController(navController, AppBarConfiguration(navController.graph))
    }
    

    Don't forget to override the onSupportNavigateUp() method as suggested in the doc:

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration)
                || super.onSupportNavigateUp()
    }