charles young
charles young

Reputation: 2289

Sherlock option menu item does not respond

The is a problem that affects platforms prior to 3.0, i.e. when Sherlock acts as a proxy to provide the action bar menu items.

I have a Fragment Activity that contains two fragments, each with its own set of options menus. When the activity starts the first fragment tab is shown and its menu items work normally. However, the first time that I switch to the other tab its menu items do not respond. If I switch back to the first tab and select the other tab again they start to fire normally.

It looks like this is a known problem. Check out the discussion here.

My workaround for now is to detect the first time that the second tab is selected and programatically switch back to the first tab. This forces the user to select the second tab again, but from that point on it works normally, as long as the user stays in that activity.

I am wondering if anyone else has found a more elegant solution to this problem. Thanks!

First fragment:

  @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      moveMenuItem   = menu.add(Flashum.MENU_GROUP_MULTI, Flashum.MOVE_FLASHES_ID, 0, R.string.move_flashes);
      cloneMenuItem  = menu.add(Flashum.MENU_GROUP_MULTI, Flashum.CLONE_FLASHES_ID, 0, R.string.clone_flashes);
      deleteMenuItem = menu.add(Flashum.MENU_GROUP_MULTI, Flashum.DELETE_FLASHES_ID, 0, R.string.delete_flashes);
      moveMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
      moveMenuItem.setIcon(R.drawable.move2red);
      cloneMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
      cloneMenuItem.setIcon(R.drawable.hard_drive_clone);
      deleteMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
      deleteMenuItem.setIcon(R.drawable.delete);
   }

Second fragment:

   @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      saveMenuItem = menu.add(Flashum.MENU_GROUP_SAVE, Flashum.SAVE_CHANGES_ID, 0, R.string.save_changes);
      saveMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
      saveMenuItem.setIcon(R.drawable.save);
      menu.setGroupVisible(Flashum.MENU_GROUP_SAVE, true);

      recMenuItem = menu.add(Flashum.MENU_GROUP_REC, Flashum.RECORD_ID, 0, R.string.record);
      recMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
      recMenuItem.setIcon(R.drawable.microphonehot);
   }

Upvotes: 0

Views: 889

Answers (2)

FranMowinckel
FranMowinckel

Reputation: 4343

Instead of detecting when the second tab is selected, switch the fragments programmatically in the onCreate of your Activity using a post delayed method. For example I have 3 fragments and I switch them in reverse order (2, 1, 0) and it fools your users with a nice animation at the beginning of the Activity:

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB){
  mIndicator.setCurrentItem(2);
  mIndicator.postDelayed(new Runnable() {
public void run() {
  mIndicator.setCurrentItem(1);
  mIndicator.postDelayed(new Runnable() {
    public void run() {
      mIndicator.setCurrentItem(0);
  }}, 100);
}}, 100);
}

Upvotes: 0

Michał Z.
Michał Z.

Reputation: 4119

Try to clear your menu before you inflate it in your fragments. So in your fragments in onCreateOptionsMenu methods call menu.clear(); at the beginning and then inflate your menu. Something like this:

   @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

  super.onCreateOptionsMenu(menu, inflater); 
  menu.clear();

  saveMenuItem = menu.add(Flashum.MENU_GROUP_SAVE, Flashum.SAVE_CHANGES_ID, 0, R.string.save_changes);
  saveMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
  saveMenuItem.setIcon(R.drawable.save);
  menu.setGroupVisible(Flashum.MENU_GROUP_SAVE, true);

  recMenuItem = menu.add(Flashum.MENU_GROUP_REC, Flashum.RECORD_ID, 0, R.string.record);
  recMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
  recMenuItem.setIcon(R.drawable.microphonehot);

}

Try it in both of your fragments.

Upvotes: 1

Related Questions