Change ActionBar title using Fragments

社会主义新天地 提交于 2019-12-20 10:30:21

问题


  • 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!