1

I have a navigation drawer that links three fragments. All fragments have an ActionBar with different titles. Fragment A and B have no tabs, while Fragment C has tab 1 and tab 2. Navigating through them works fine and shows different ActionBar titles, but when I navigate from C to the other fragments, the tabs are still displayed in Fragment A and B's ActionBar. How do I "hide" the tabs when am navigating from C to Fragment A and B ?

Thanks

Switching between fragments

switch (possition) {
        case 0:
            fragment = new FragmentA();
            break;
        case 1:
            fragment = new FragmentB();
            break;
        case 2:
            fragment = new FragmentC();
            break;

             default: 
            break;
         }
         FragmentManager frgManager = getSupportFragmentManager();
         frgManager.beginTransaction().replace(R.id.content_frame, fragment)
            .commit();

Fragment C

 private String[] tabs = { "1", "2"};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_utilities, container, false);
    viewPager = (ViewPager) rootView.findViewById(R.id.pager);
    actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();
    mAdapter = new TabsPagerAdapter(getActivity().getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(UtilitiesFragment.this));
    }
Galileo
  • 321
  • 4
  • 7

2 Answers2

0

Try simply setting your navigation mode to be the non-tabbed variety...

setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

...and then setting it back to the tabbed mode when relevant.

Another option is to simply remove the tabs via the removeTab method on the ActionBar and then add them back again when relevant.

One thing to note is that this navigation method has been deprecated as of API level 21 and it is recommended that you use a different approach/pattern.

Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • using 'setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);' on non tab fragments and 'setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)' on the fragment with tabs worked. For now I can use it as a temporary fix but I would like to know how I can hide tabs for [this](http://stackoverflow.com/a/27729494/3729235) – Galileo Jul 27 '15 at 10:34
0

Actually the second time I'm suggesting this today:Use a TabLayout inside the fragment instead of the ActionBar tabs.

https://developer.android.com/reference/android/support/design/widget/TabLayout.html

Raanan
  • 4,777
  • 27
  • 47