Contextual Action bar (Action mode) always gets destroyed when physical back button is pressed

378 Views Asked by At

So, I don't want to close Action mode on certain condition. so tried to do following

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

if (my condition) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        return true; // consuming back event here. and yes it gets called on backpress meaning event gets consumed successfully here
    }   
}
    return super.dispatchKeyEvent(event);
}

but still it always closes CAB every time i press back button. what could be the reason ?

1

There are 1 best solutions below

1
On

Add this in your styles.xml file

<!--  It should be true otherwise action mode will not overlay toolbar -->
        <item name="windowActionModeOverlay">true</item>

        <!--  For Custom Action Mode Background Color/Drawable -->
        <item name="actionModeBackground">@color/colorAccent</item>