androidandroid-actionbarandroid-support-libraryandroid-actionmode

How to disable actionbar back button in ActionMode


I use ActionMode to edit some content, those three buttons on the right do everything needed, and the back/home/up button on the left becomes redundant.

Screenshot, see that button on the left

Here's the ActionMode

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.editormenu, menu);
        return true;
    }
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_cancel:
                selectedView.onCancelEditing();
                selectedView.enableEditMode(selectedView,false);
                mode.finish(); 
                return true;
            case R.id.action_remove:
                selectedView.enableEditMode(selectedView,false);
                relContainer.removeView(selectedView);
                mode.finish();
                return true;
            case R.id.action_done:
                selectedView.enableEditMode(selectedView,false);
                mode.finish();
                return true;
            default:
                return false;
        }
    }
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mActionMode = null;
    }
};

I tried to use that back/home/up button as "cancel" button but it seems that it's id is not android.R.id.home nor R.id.home

so question is: How do I remove/disable this button, OR How do I make use of it?


Solution

  • Just call dispatchKeyEvent method and perform action on it: Have a look on the following example:

    @Override 
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(mActionModeIsActive) { 
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
               // handle your back button code here 
               return true; // consumes the back key event - ActionMode is not finished 
            } 
        } 
        return super.dispatchKeyEvent(event);
    } 
    

    For further info:

    Prevent to cancel Action Mode by press back button