问题
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
?
回答1:
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>
来源:https://stackoverflow.com/questions/12730404/contextual-action-mode-custom-behavior