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(); }
Solution: Save required information as an instance variable in calling activity. Then pass that instance variable into your 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.
onStart() makes the fragment visible to the user (based on its containing activity being started).
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.
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); } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With