Christopher Orr Christopher Orr - 1 year ago 108
Android Question

Handling ActionBar title with the fragment back stack?

I have an

where I load in a
and, upon clicking, it drills down a level and a new type of
is shown, replacing the original one (using the
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

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
knows about, so the title isn't restored.

I've vaguely read about
, 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(, f);

Answer Source

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

public void onResume() {
    // Set title

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

public void setUserVisibleHint(boolean isVisibleToUser) {
    if(isVisibleToUser) {
        // Set title
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download