Question

I have implemented tabs using the actionbar, inside of a fragment, called TabHostFragment

public class TabHostFragment extends Fragment {

public TabHostFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    if(savedInstanceState == null){
    }

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_tab_host, null, false);
    ViewPager mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
    ActionBar actionbar = this.getActivity().getActionBar();

    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionbar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    TabsAdapter mTabsAdapter = new TabsAdapter(this, mViewPager);
    mTabsAdapter.addTab(actionbar.newTab().setText("Summoner"),
            SummonerFragment.class, null);
    mTabsAdapter.addTab(actionbar.newTab().setText("Games"),
            GameHistoryListFragment.class, null);
    mTabsAdapter.addTab(actionbar.newTab().setText("League"),
            LeagueFragment.class, null);
    mTabsAdapter.addTab(actionbar.newTab().setText("Masteries"),
            MasteriesFragment.class, null);
    mTabsAdapter.addTab(actionbar.newTab().setText("Runes"),
            RunesFragment.class, null);
    return rootView;
}

}`

The TabsAdapter class is here

public class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

static final class TabInfo {
    private final Class<?> clss;
    private final Bundle args;

    TabInfo(Class<?> _class, Bundle _args) {
        clss = _class;
        args = _args;
    }
}

public TabsAdapter(Fragment frag, ViewPager pager) {
    super(frag.getChildFragmentManager());
    frag.getChildFragmentManager();
    mContext = frag.getActivity();
    mActionBar = frag.getActivity().getActionBar();
    mViewPager = pager;
    mViewPager.setAdapter(this);
    mViewPager.setOnPageChangeListener(this);
}

public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
    TabInfo info = new TabInfo(clss, args);
    tab.setTag(info);
    tab.setTabListener(this);
    mTabs.add(info);
    mActionBar.addTab(tab);
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return mTabs.size();
}

@Override
public Fragment getItem(int position) {
    TabInfo info = mTabs.get(position);
    return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}

@Override
public void onPageSelected(int position) {
    mActionBar.setSelectedNavigationItem(position);
}

@Override
public void onPageScrollStateChanged(int state) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

}

The main activity holds a NavigationDrawer, which loads TabHostFragment into the activity

        if(position == 1){
        fragment = new TabHostFragment();
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        if(!fragmentManager.findFragmentById(R.id.list_container).getClass().equals(TabHostFragment.class)){
            fragmentManager.beginTransaction().replace(R.id.list_container, fragment).addToBackStack(null).commit();
            if(mTwoPane){
                FrameLayout fl = (FrameLayout) findViewById(R.id.detail_container);
                fl.removeAllViews();
                findViewById(R.id.detail_container).setVisibility(View.GONE);
            }
        }

    }

When leaving this fragment and coming back, i.e. selecting something else in the navigation drawer, and then either pressing back or selecting the TabHostFragment in the drawer, the tab titles get duplicated. Can't post pictures yet so enter image description here

I'm not sure why this is happening, and couldn't find a similar issue anywhere on stackoverflow.

Was it helpful?

Solution

It seems that the tabs are not getting cleared from the ActionBar when you've re-entered your particular view.

Try using mActionBar.RemoveAllTabs() before adding any more tabs. In the constructor of your TabsAdapter perhaps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top