Search code examples
androidandroid-fragmentsandroid-adapterpagertabstrip

SlidingTabLayout and Actionbar title


I'm using a SlidingTabLayout (from the Google sources) in my app to navigate between different fragments. I can swipe between contents just fine, but the problem I'm facing is the title of the Action bar that is acting weird.

Suppose that I have two tabs with two titles ('First Title' and 'Second Title') :

| Action bar       |

| First Title | Second Title |

When I first enter the Fragment containing the SlidingTabLayout, the title of the Actionbar is like this :

| Firs...      |

| First Title | Second Title |

When I swipe (to the second tab for example), the title of the actionbar becomes :

| Second Title       |

| First Title | Second Title |

And stays like this. It seems that the Actionbar takes the title of the last Fragment loaded when I swipe at least once.

What I want is this :

I want to show a 'Main Title' in the Actionbar that never changes no matter what the title in the SlidingTabLayout is.

Here are some portions of my code:

** Fragment containing the SlidingTabLayout:

private String mainTitle;
....

@Override
public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);

    // The mainTitle is given to this fragment by another fragment
    Bundle args = getArguments();
    if (args != null) {
        mainTitle = args.getString("TITLE");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.layout, container, false);
    mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
    mSlidingLayout = (SlidingTabLayout) rootView.findViewById(R.id.sliding_layout);
    List<Fragment> listFragments = new ArrayList<>();
    List<String> listTitles = new ArrayList<>();
    for (int i = 0; i < mSize; i++) {
        Bundle bundle = new Bundle();
        ....
        listFragments.add(Fragment.instanciate(getActivity(), CustomFragment.class.getName(), bundle));
        listTitles.add("...");
    }
    mViewPager.setAdapter(new PagerAdapter(getChildFragmentManager(), listFragments, listTitles));

    mSlidingLayout.setDistributedEvenly(true);
    mSlidingLayout.setViewPager(mViewPager);

    return rootView;
}

@Override
public void onResume() {
    super.onResume();
    // I TRY TO SET THE TITLE HERE !!
    getActivity().getSupportActionBar().setTitle(mainTitle);
}

** Adapter:

class PagerAdapter extends FragmentPagerAdapter {
    List<Fragment> fragments;
    List<String> titles;
    ...
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = fragments.get(position);
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // maybe the problem is in this method.
        // this method is supposed to provide the titles for the tab
        // but maybe it's also changing the actionbar title
        return titles.get(position);
}

I'm setting the title of the ActionBar in the onResume method every time I enter a Fragment.

Thanks !

EDIT 1 :

I tried using the new SlidingTabLayout which I've got from the Google I/O, and the result is still the same !

It seems that the Title in the ActionBar is a hint at first, and then it changes to the last loaded fragment when I swipe to another fragment.

It's like it's loading fragment, and each time a fragment is loaded, the title in the ActionBar is overridden with the title of that fragment.

EDIT 2 :

I changed my code to post the latest version of it (I'm using now a SlidingTabLayout) and to show how I get my mainTitle.


Solution

  • Found it !

    It was really dumb of me (it explains why others didn't have this problem)

    I was setting the title also in each of the Fragments (even those contained in the Tabs), so when I swiped, the onResume on those Fragments was called and it changed the title in the ActionBar...

    Thank you all for the help, I appreciate it !