Contextual Action Mode custom behavior
Asked Answered
S

1

6

In android developer's menu guide it is mentioned that:

The action mode is disabled and the contextual action bar disappears when the user deselects all items, presses the BACK button, or selects the Done action on the left side of the bar.

Technically, it means that mActionMode.finish(), the BACK button press, or the Done action selection call ActionMode.Callback onDestroyActionMode() method.

My question is how to perform a custom action (for example Toast("Action mode exit by Done select")) when the user selects Done, and another action (for ex. Toast("Action mode exit by BACK")) when the user press BACK?

Shocker answered 4/10, 2012 at 15:13 Comment(2)
Monitor the current state of the CAB(open/not open) and the number of selected items in the list and you could determine which method was used to close the CAB.Tendinous
Alexiosdev answered to a similar question [here][1]. [1]: https://mcmap.net/q/415101/-how-to-recognize-whether-the-done-button-is-clicked-in-actionmodeStrigose
L
2

One approach you can take to solving this problem is using a Theme to hide the done button from action modes you create. Then, you simply add your own Done button to every action mode you create. Obviously then you can track whether onDestroyActionMode was called because of your done button being hit or by the back button. Here is a Theme that you could apply to the activities that you need to accomplish this with.

 <style name="HideActionModeCloseTheme" parent="@android:style/Theme.DeviceDefault">
    <item name="android:actionModeCloseButtonStyle">@style/NoCloseButton</item>
</style>

<style name="NoCloseButton" parent="@android:style/Widget.DeviceDefault.ActionButton.CloseMode">
    <item name="android:visibility">invisible</item>
</style>
Laflam answered 18/4, 2013 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.