0
  1. i am using actionsherlockbar Library + JfenisteinSlidingMenu

question : how to add dinamically optionmenu located in actionbar in every fragment ?

i tried below, but optionmenu is same in every page.. how can i do this ?

public class FragmentChangeActivity extends BaseActivity  {

private Fragment mContent;
private String json = null;

public FragmentChangeActivity() {
    super(R.string.changing_fragments);
}

@SuppressWarnings("unused")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set the Above View
    if (savedInstanceState != null)
        mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
    if (mContent == null)
        mContent = new ColorFragment(android.R.color.white);    




    // set the Above View
    setContentView(R.layout.content_frame);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame, mContent)
    .commit();

    // set the Behind View
    ColorMenuFragment colorMenuFragment = new ColorMenuFragment();
    LeftMenuFragment leftMenu = new LeftMenuFragment();
    setBehindContentView(R.layout.menu_frame);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.menu_frame, colorMenuFragment)
    .commit();

    // customize the SlidingMenu
    getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    getSupportFragmentManager().putFragment(outState, "mContent", mContent);
}

public void switchContent(Fragment fragment) {
    mContent = fragment;
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame, fragment)
    .commit();
    getSlidingMenu().showAbove();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        toggle();
        return true;
    case R.id.github:
        Utils.goToGitHub(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

2 Answers2

0

You do not add option menu in Fragments but in Fragment Activity and Fragment Activity will act like container which contain fragments.

Androider
  • 2,884
  • 5
  • 28
  • 47
0

In your every fragment inflate option menu also? I don't know about lib but hope this may help you this working well for me. just return false in your onOptionsItemSelected see this example

in my activity

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.action:
    //Do something
            break;

        default:break;
    }
    return false;
}

and in my Fragment

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

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()){
      case R.id.fragment_action:
         //Do something
          break;
  }
    return true;
}
siwarak
  • 177
  • 13