Android: Showing Action Bar menu items depending on ViewPager

You can implement onCreateOptionsMenu() in your Fragment and set 'setHasOptionsMenu(true)' for the fragment


invalidateOptionsMenu make the system calls the method onPrepareOptionsMenu, so you can override this method as follows:

public boolean onPrepareOptionsMenu(Menu menu) {
      int pageNum = getCurrentPage();
      if (pageNum == 1) {
         menu.findItem(R.id.menu_search).setVisible(true);

      }
      else {
         menu.findItem(R.id.menu_search).setVisible(false);
      }
   }

public void onPageSelected(int pageNum) {
    invalidateOptionsMenu();
}