Change ActionBar header using snippets

  • I am using Actionbar.
  • I have a Favorite Call Fragment, where I save some contacts as favorites.
  • When you click on one contact, it transfers me to another fragment and puts the contact number in the editing text.
  • The new piece is called Call Transfer.

So my problem is that when a user clicks on a contact, it leads me to another fragment, but the title in the action bar is still with a favorite title and not a new one, how to change this title?

I already tried using setTitle on the click method, but still does not work.

+15
source share
3 answers

In your activity:

 public void setActionBarTitle(String title) { getSupportActionBar().setTitle(title); } 

And in your fragment (you can put it onCreate or onResume):

  public void onResume(){ super.onResume(); // Set title bar ((MainFragmentActivity) getActivity()) .setActionBarTitle("Your title"); } 
+55
source

In your fragment

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { getActivity().setTitle("Team B"); View rootView = inflater.inflate(R.layout.fragment_team_b, container, false); return rootView; } 
+20
source

If you are using navigation components from Android Jetpack. The action bar reads the Label attribute for the fragment name. I'm not sure if this is the correct fix, but if you change the label text in the navigation editor, it will be read by the supportActionBar configured in the action in which the fragments are placed. Navigation Editor Attributes Tab

0
source

Source: https://habr.com/ru/post/982159/


All Articles