Jeison Duran Jeison Duran - 5 months ago 49
Android Question

Change ActionBar title using Fragments

First let me explain something, so you guys can understand better my problem:


  • 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 click 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 can i achieve to change that title?

I have already try to use setTitle on the click method but still not working.

Thanks so much!
Sorry for my bad english!

Answer

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

}