Navigation drawer contains 4 items and each item contains 4 actionbar tabs ,while switching between menu items means if i click on menu item 2 then number of actionbar items changing to 8 and if i click on menu item3 then number of actionbar tabs changing to 12 ,how o stop this repeation of tabs.here is my code
public class TopicsFragment extends Fragment {
public TopicsFragment() {
}
// Declare Tab Variable
ActionBar.Tab AllTopics, NewContent, StaffPicks, Popular, Recommended;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
Fragment fragmentTab3 = new FragmentTab3();
Fragment fragmentTab4 = new FragmentTab2();
Fragment fragmentTab5 = new FragmentTab1();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup tabs,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_topics, tabs, false);
ActionBar actionBar = ((ActionBarActivity) getActivity())
.getSupportActionBar();
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
AllTopics = actionBar.newTab().setIcon(R.drawable.tab1);
NewContent = actionBar.newTab().setIcon(R.drawable.tab1);
StaffPicks = actionBar.newTab().setIcon(R.drawable.tab1);
Popular = actionBar.newTab().setIcon(R.drawable.tab1);
Recommended = actionBar.newTab().setIcon(R.drawable.tab1);
// Set Tab Listeners
AllTopics.setTabListener(new TabListener(fragmentTab1));
NewContent.setTabListener(new TabListener(fragmentTab2));
StaffPicks.setTabListener(new TabListener(fragmentTab3));
Popular.setTabListener(new TabListener(fragmentTab2));
Recommended.setTabListener(new TabListener(fragmentTab2));
// Add tabs to actionbar
actionBar.addTab(AllTopics);
actionBar.addTab(NewContent);
actionBar.addTab(StaffPicks);
actionBar.addTab(Popular);
actionBar.addTab(Recommended);
return rootView;
}
}
That problem is that you are adding tabs constantly when you go back in your TopicsFragment
which will call onCreateView
again and executing the adding of tabs
to the actionbar thus adding another set of tabs to the current tabs.
solution:
you need to check first if the number of tabs is zero in your actionbar.
sample:
actionBar.removeAllTabs();
if(actionBar.getTabCount() == 0)
{
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
AllTopics = actionBar.newTab().setIcon(R.drawable.tab1);
NewContent = actionBar.newTab().setIcon(R.drawable.tab1);
StaffPicks = actionBar.newTab().setIcon(R.drawable.tab1);
Popular = actionBar.newTab().setIcon(R.drawable.tab1);
Recommended = actionBar.newTab().setIcon(R.drawable.tab1);
// Set Tab Listeners
AllTopics.setTabListener(new TabListener(fragmentTab1));
NewContent.setTabListener(new TabListener(fragmentTab2));
StaffPicks.setTabListener(new TabListener(fragmentTab3));
Popular.setTabListener(new TabListener(fragmentTab2));
Recommended.setTabListener(new TabListener(fragmentTab2));
// Add tabs to actionbar
actionBar.addTab(AllTopics);
actionBar.addTab(NewContent);
actionBar.addTab(StaffPicks);
actionBar.addTab(Popular);
actionBar.addTab(Recommended);
}