0

I want to change the "android:label" in Fragment

I tried to run the following code to change label, but It failed.

val graph = findNavController().graph
graph.label = "测试"
findNavController().graph = graph
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Saxxhw
  • 7
  • 1
  • 4
  • Why are you trying to change the label? – ianhanniballake Jun 18 '19 at 03:35
  • Some information needs to be displayed dynamically in the title.For example,A Fragment for displaying device information,I need change the label to equipment name.(Translate by Google) – Saxxhw Jun 18 '19 at 03:44

3 Answers3

3

As per the Navigation UI documentation, the NavigationUI methods, such as the setupActionBarWithNavController() method rely on an OnDestinationChangedListener, which gets called every time you navigate() to a new destination. That's why the label is not instantly changed - it is only updated when you navigate to a new destination.

The documentation does explain that for the top app bar:

the label you attach to destinations can be automatically populated from the arguments provided to the destination by using the format of {argName} in your label.

This allows you to update the string you use for your label (say R.string.destination_label) to be in the form of

<string name="destination_label">You are on {destination}</string>

By using that same argument on your destination, your title will automatically be populated with the correct information.

Of course, if you don't have an argument that you can determine ahead of time, then you'd want to avoid setting an android:label on your destination at all and instead manually update your action bar's title, etc. from that destination.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • It helped me solve the problem.Thank you very much for your help. – Saxxhw Jun 18 '19 at 04:14
  • How do i pass {argName} to the fragment if my fragments are tied to the toolbar icons automatically beacause i have the same ids in both? – Puntogris Feb 26 '20 at 21:05
  • Could you look at my question? [Is it possible to make label attribute flexible in Navigation Component?](https://stackoverflow.com/questions/72127696/is-it-possible-to-make-label-attribute-flexible-in-navigation-component) – Taha Sami May 05 '22 at 13:08
1
navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
        destination.setLabel("Bruno is here");

    });
user2718075
  • 333
  • 2
  • 4
  • 15
  • While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. – John Conde Mar 03 '21 at 23:38
0

Just add following lines of codes inside the hosting activity onCreate() function.

  1. Get reference of NavController, here I am using NavHostFragment for getting reference of NavController, you can use other ways too.
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_container);

NavController navController= navHostFragment.getNavController();
  1. Add addOnDestinationChangedListener to NavController and change label based on data item received in arguments.
navController.addOnDestinationChangedListener( new NavController.OnDestinationChangedListener() {
                @Override
                public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
                    if (destination.getId() == R.id.fragmentID) {
                        if (arguments != null) {
                            DataItem item = FragmentArgs.fromBundle(arguments).getItem();
                            if (item != null)
                                destination.setLabel(getString(R.string.your_label));
                            else
                                destination.setLabel(getString(R.string.other_label));
                        }
                    }

                }
            });
PraveenK
  • 51
  • 1
  • 4