Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ActionBar title using Fragments

Tags:

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

like image 883
Jeison Duran Avatar asked Feb 08 '15 02:02

Jeison Duran


People also ask

How to change ActionBar title in fragment?

To set the title for your ActionBar , while using a custom layout, in your Fragment you'll need to call getActivity(). setTitle(YOUR_TITLE) .

How to change title from fragment?

you can change the title by overriding the onResume method. And for the safe side, you can also override setUserVisibleHint and change the title when fragment isVisibleToUser .

Can We have fragments without activity?

Fragment can't be initiated without Activity or FragmentActivity.


1 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");  } 
like image 166
NOT_A_PROGRAMMER Avatar answered Sep 22 '22 13:09

NOT_A_PROGRAMMER