Change ActionBar title using Fragments

2019-03-12 16:00发布

  • 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.

2条回答
地球回转人心会变
2楼-- · 2019-03-12 16:40

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");

}
查看更多
等我变得足够好
3楼-- · 2019-03-12 16:51

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;
}
查看更多
登录 后发表回答