问题
- I'm using Actionbar.
- I have a Fragment call Favorites where i save some contacts as favorites.
- When you click on one contact it takes me to another fragment an put the contact number on a editText.
- The new fragment is call Transfers.
So my problem is that when the user clicks the contact, that takes me to the other fragment but the title on the action bar still with the favorite title and not the new one, how to change that title?
I have already try to use setTitle on the click method but still not working.
回答1:
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");
}
回答2:
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;
}
回答3:
If you are using Navigation Components from Android Jetpack. The action bar reads the Label attribute for the fragment name. Not sure if this is a proper fix, but if you change the Label text in the Navigation Editor, it will be read by the supportActionBar that is set up in the Activity hosting the fragments.
来源:https://stackoverflow.com/questions/28389841/change-actionbar-title-using-fragments