Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling ActionBar title with the fragment back stack?

I have an Activity where I load in a ListFragment and, upon clicking, it drills down a level and a new type of ListFragment is shown, replacing the original one (using the showFragment method below). This is placed on the back stack.

At the beginning, the activity shows the default title in the action bar (i.e. it's set automatically based on the application's android:label).

When showing the list for the next level in the hierarchy, the name of the item clicked on should become the action bar's title.

However, when pressing Back, I would like the original default title to be restored. This isn't something FragmentTransaction knows about, so the title isn't restored.

I've vaguely read about FragmentBreadCrumbs, but this seems to require using a custom view. I'm using ActionBarSherlock and would prefer to not have my own custom title view.

What is the best way of doing this? Is it possible without a load of boilerplate code and having to keep track of the titles shown along the way?


protected void showFragment(Fragment f) {   FragmentTransaction ft = getSupportFragmentManager().beginTransaction();   ft.replace(R.id.fragment_container, f);   ft.addToBackStack(null);   ft.commit(); } 
like image 329
Christopher Orr Avatar asked Nov 20 '12 11:11

Christopher Orr


People also ask

How can I maintain fragment state when added to the back stack?

Solution: Save required information as an instance variable in calling activity. Then pass that instance variable into your fragment.

What is back stack in fragment?

The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.

Which method is called to instantiate a fragment's user interface view?

onStart() makes the fragment visible to the user (based on its containing activity being started).

What are fragments android?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.


2 Answers

In every fragment and every activity I change the title like this. This way the active title will always be correct:

@Override public void onResume() {     super.onResume();     // Set title     getActivity().getActionBar()         .setTitle(R.string.thetitle); } 

There is some cases where onResume isn't called inside fragments. In some of these cases we can use:

public void setUserVisibleHint(boolean isVisibleToUser) {     super.setUserVisibleHint(isVisibleToUser);     if(isVisibleToUser) {         // Set title         getActivity().getActionBar()             .setTitle(R.string.thetitle);     } } 
like image 82
Warpzit Avatar answered Oct 11 '22 08:10

Warpzit


As the original answer is quite old, this might come of help as well. As the documentation states, one might want to register a listener to listen on the back stack changes in the hosting Activity:

getSupportFragmentManager().addOnBackStackChangedListener(         new FragmentManager.OnBackStackChangedListener() {             public void onBackStackChanged() {                 // Update your UI here.             }         }); 

Then, identify the situation in the callback method and set a proper title, without accessing the ActionBar from the Fragment.

This is a more elegant solution as the Fragment doesn't have to know about the ActionBar existence and Activity is usually the place that is managing the backstack so having it handled over there seems to be more appropriate. Fragment should at all time be considered only by its own content, not the surroundings.

More on the topic in the documentation.

like image 44
Maciej Pigulski Avatar answered Oct 11 '22 08:10

Maciej Pigulski