0

I am using fixed tabs + swipe inside a FragmentActivity. I have added a menu in action bar. I want that when the menu is clicked it should open class based on the current tab that is selected.

static TabHost myTabs;
-
-
-
myTabs = getTabHost();

This shows an error asking me if it shouls create a method called getTabHost(). I am assuming its because I am using FragmentActivity instead of Activity. So how do I know which tab is selected?

EDIT

As told below I have done this

@Override
public void onAttachFragment(Fragment fragment){
    super.onAttachFragment(fragment);
    tabValue=fragment.getId();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.one, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (tabValue) {
    case 0:
        Toast.makeText(BarcodeActivity.this, "HELLO", Toast.LENGTH_LONG).show();
    case 1:
        Intent i = new Intent(BarcodeActivity.this, NewCodeAdder.class);
        startActivity(i);
        break;
    }
    return true;
}

But still nothing happens when I click on the menu add option.

vishalmullur
  • 1,094
  • 4
  • 14
  • 32

1 Answers1

0

Check this out, it has everything from id to finding names of tabs:

Get the current fragment object

Community
  • 1
  • 1
KickAss
  • 4,210
  • 8
  • 33
  • 41