Shadow
Shadow

Reputation: 4743

How to set toolbar title from fragment using Navigation UI Component

I have an activity with a toolbar from a default layout in Android Studio and I can easily change the title in the toolbar through the following method:

    Bundle bundle = new Bundle();
    bundle.putString("toolbarTitle", "A new title");

    NavHostFragment
        .findNavController(this)
        .navigate(R.id.action_navigation_conversation_to_placeholder, bundle);

Where safe args is already implemented for toolbarTitle in the navigation menu layout.

However now I am trying to change the title from the fragment that is opened via the method above, for example;

I do know how to update it without using Navigation UI Components, but I want to use them if it is possible.

Other similar questions are only related to the example I already provided above for when a new fragment is being opened or by suggesting the use of the typical method that does not make use of the Navigation UI Components, none cover this specific scenario.

Upvotes: 1

Views: 2715

Answers (2)

Francisco Arenas
Francisco Arenas

Reputation: 126

You can change the the title by accesing the supportActionBar from the activity:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    (requireActivity() as AppCompatActivity).supportActionBar?.title = "Your Title"
}

Upvotes: 6

Shadow
Shadow

Reputation: 4743

It seems like this is not possible with the only way to achieve this by doing it directly without using the Navigation Architecture Components.

Upvotes: 0

Related Questions