Search code examples
javaandroidandroid-fragmentsactionbarsherlocknavigation-drawer

Different actionbar item for different tabs, Actionbarsherlock


I have a class which extends SherlockFragment, which has a actionbarItem "filter"

TabbedFragment.java

public class TabbedFragment extends SherlockFragment{

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
 }

 @Override
 onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.activity,container, false);

    mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("TAB 1"),
            Tab1.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("TAB 2"),
            Tab2.class, null);        

    for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++) 
    {
        TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
        tv.setTextColor(Color.parseColor("#ffffff"));
    } 
    return rootView;
 }

 @Override
 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.post_filter, menu);
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menuOption_filter:
            // To-Do
    default:
        break;
    }
    return true;
  }
}

I have two tabs, both extends ListFragment

Tab1.java:

public class Tab1 extends ListFragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // TO-DO
  }
}

Tab2.java:

public class Tab2 extends ListFragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // TO-DO
  }
}

This will create ActionBar like this:

 ---------------------
|              Filter | <--- ActionBarItem
 ---------------------
|  TAB1   |    TAB2   |
 ---------------------

Now i want to show the filter only when the user is on TAB1 and when user selects TAB2 the "filter" should be hidden.

Solution i tried:

Instead of creating a menu in "TabbedFragment" class, i created a menu in Tab1 but the onCreateOptionsMenu() method in Tab1 is not getting called (I tried setHasOptionsMenu(true) in OnCreate() of Tab1, still no luck)

P.S. TabbedFragment class extends SherlockFragment, I don't want it to extend with Activity/SherlockFragmentActivity.

Please HELP!!!


Solution

  • That flow should actually work, else you can have a workaround to manipulate your menu.

    • Add OnTabChangeListener to your tab host
    • Save the selected tab state
    • Call invalidateOptionsMenu()
    • onCreateOptionsMenu method will be called
    • Based on the selected tab, manage your menu item visibility