how to detect if actionmode already present
Asked Answered
P

1

9

I have several widgets in a view, each needing its own ActionMode. I see that the ActionMode does not dismiss automatically when the user taps outside the action bar. Thus, it is easily possible for the user to start an ActionMode for one control, then tap (longclick in my case) another control and stack a second ActionBar on top of the first. This causes programming logic havoc.

I can keep track of the current ActionMode with an activity-level member variable and dismiss the current one if a new one is needed. Howewver, this is making my code messy to read and maintain. And further, I'd prefer to dismiss it immediately when the user taps anything outside the action bar.

Any suggestions on a good way to handle this?

Parietal answered 3/10, 2012 at 14:21 Comment(0)
B
8

I was looking for a solution of this problem some time ago and as I know you couldn't track it without saving current action-mode state in a global variable. However I don't think that one variable with proper name would make your code messy.

Badgett answered 3/10, 2012 at 14:29 Comment(5)
Ok, but messiness aside, let's say the user clicks on an EditText that you haven't subclassed. How do you dismiss the now-inappropriate action bar?Parietal
Ok, if I understood you right... I made it like that: implemented global variable ActionMode mMode;, filled it with current action mode in a onCreateActionMode(...) method, and then just call mMode.finish() when I need to exit from current action-modeBadgett
Right, I get that. But how do you know when to call finish()? That is, there are many places a user can click; do you subclass every object and check for MotionEvent.ACTION_DOWN and then call finish()? That would be highly error prone, these bits of code would be peppered all over the place.Parietal
It depends on situation, for example you can add transparent view match_parent,match_parent to your main layout under control elements and just handle user's clicks on itBadgett
That sounds good. If I understand correctly, this is still kind of tricky: the transparent view needs to check where the click is and somehow know if it is over the widget that initiated the action mode; if not, then it "finish"es the action mode. I'll mark your answer as correct, because it sounds good and I have a hunch there isn't a better way.Parietal

© 2022 - 2024 — McMap. All rights reserved.