I have a view pager with each fragment having its own options menu which is inflated when the fragment comes is current one in the view pager however, it is not working as expected. Sometimes the correct options menu appears sometimes the adjacent fragments menu appears. I have read from other SO posts and other sites that it is due to view pager holding the current page and the two adjacent pages to be in "current" view thus, the options menu are gobbled together as one or something like that which creates the problem. I have solved the gobbling problem by calling menu.clear() before inflating the new menu. but the menu is still not correctly loaded correctly sometimes(well most of the times, actually).
Some suggestions have been made on other sites to change the view pager classes but I am not sure if that is the correct way. Can anyone please help direct to a solution or give some advice on how to go around this problem.
Code I am using:
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.A_fragment_optionmenu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
/* switch (item.getItemId()) {
case R.id.insert_tag:
FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.replace(R.id.buy_root_frame, new B_insert_tags());
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
trans.addToBackStack(null);
trans.commit();
return true;
default:
}*/
return super.onOptionsItemSelected(item);
}
ok used onprepareoptionsmenu on the activity where view pager was and then used on pageselected to inflate the right menu. works but with a small lag (unnoticeable though).