1

I have a Fragment, in that, i declare a FragmentTabHost.

public class ComFragment extends Fragment{

    ...
    private FragmentTabHost mTabhost;
    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ...
        return mTabhost;
    }
    ...
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.com_tabhost, menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        ...
        switch (item.getItemId()) {
        case R.id.action_clear:

            break;
        case R.id.action_close:
            int index = mTabhost.getCurrentTab();
            /*\ How can i remove current tab at here?*/
            break;
        default:
            break;
        }
        return true;
    }
}

How can I remove current tabspec when i click on item?

yuva ツ
  • 3,707
  • 9
  • 50
  • 78
sonpx
  • 27
  • 3

1 Answers1

2

Unfortunately, there's no way of accessing the TabSpec once you add it to the FragmentTabHost. You'll have to define some datastructure (probably a Map or derivate) in your class where you store the TabSpecs for each tab, and when you want to remove one, you'll have to call .clearAllTabs() on your FragmentTabHost and loop your Map to add again all TabSpecs except the one you want to remove.

nKn
  • 13,691
  • 9
  • 45
  • 62
  • I have refered follow this link: http://stackoverflow.com/questions/3299845/how-to-remove-tab-from-tabhost I tried: mTabhost.getTabWidget().removeView(mTabhost.getTabWidget().getChildTabViewAt(index)); But it throw null pointer exception. – sonpx Apr 18 '14 at 12:55
  • 1
    Both `TabHost` and `FragmentTabHost` comes tricky when removing individual tabs from them. The question you've followed is just the first step (removing all the tabs), but afterwards you have to restore the ones you want to keep, thus you'll have to store the `TabSpec`s somewhere and handle it the way I wrote. – nKn Apr 18 '14 at 13:15