Search code examples
androidandroid-appcompatandroid-toolbarcontextual-action-bar

Contextual Action Bar messing with Toolbar actions


I'm using appcompat v7 and the support design libraries. In my activity there is a regular toolbar, which has three action buttons (RM1, RM2, RM3):

_____________________________________
                         RM1 RM2 RM3 |
_____________________________________|  

And when some items are long-clicked in a list, a contextual action bar (CAB) is shown instead of the regular toolbar. My CAB has a single action (CM).

_____________________________________
                                  CM |
_____________________________________|  

When the contextual action bar (CAB) is activated, it hides the regular toolbar. At this point if I click on the contextual menu (CM), everything is ok. But for some reason, if I click over the empty space to the left of CM, the regular menus RM1 and RM2 are shown, despite the regular toolbar being hidden by the CAB. RM1 and RM2 icons are obviously not shown when the CAB is active, but the click handlers are still in place and they are fired even though the regular toolbar is hidden. The CAB is not intercepting the clicks unless it has an action in the clicked point. If I click over CM, it is handled correctly: RM is not shown because that button is exactly below the CM menu.

Is this a bug? Any workaround?

Tested in an Android 4.1 device.


Solution

  • I consider it a bug. I have run into it myself, and isolated it. Here is a workaround, assuming you are extending AppCompatActivity:

    @Override
    public void onSupportActionModeStarted(ActionMode mode) {
        super.onSupportActionModeStarted(mode);
        rm1.setEnabled(false);
        rm2.setEnabled(false);
        rm3.setEnabled(false);
    }
    
    @Override
    public void onSupportActionModeFinished(ActionMode mode) {
        super.onSupportActionModeFinished(mode);
        rm1.setEnabled(true);
        rm2.setEnabled(true);
        rm3.setEnabled(true);
    }