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?

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101

1 Answers1

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.

user1049280
  • 5,176
  • 8
  • 35
  • 52
  • 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? – Peri Hartman Oct 03 '12 at 14:30
  • 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-mode – user1049280 Oct 03 '12 at 14:34
  • 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. – Peri Hartman Oct 03 '12 at 14:41
  • 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 it – user1049280 Oct 04 '12 at 06:50
  • 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. – Peri Hartman Oct 05 '12 at 04:42