ActionBarSherlock icon indicating copy to clipboard operation
ActionBarSherlock copied to clipboard

Second time an ActionMode is added, the close button is not clickable

Open SimonVT opened this issue 13 years ago • 21 comments

Rather strange bug in ABS4.

First time an ActionMode is shown, the close button works fine. Second time an ActionMode is shown, the close button does not react to clicks. Third+ time the ActionMode is shown, the close button works fine.

This behavior is only observed on 3.0+ when using the compat version of the ActionBar.

This is a rather simple example which should show what I'm talking about:

public class MyActivity extends FragmentActivity {

    private ActionMode mActiveActionMode;
    private ActionMode.Callback mLastCallback;

    private boolean mInActionMode;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mInActionMode = !mInActionMode;
                updateActionMode();
            }
        });
    }

    void updateActionMode() {
        if (!mInActionMode && mActiveActionMode != null) {
            mActiveActionMode.finish();

        } else if (mInActionMode && mActiveActionMode == null) {
            if (mLastCallback == null) mLastCallback = new SomeCallback();
            startActionMode(mLastCallback);
        }
    }

    class SomeCallback implements ActionMode.Callback {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mActiveActionMode = mode;
            menu.add(0, 1, 0, "Test");
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActiveActionMode = null;
            mInActionMode = false;
        }
    }
}

SimonVT avatar Jan 29 '12 12:01 SimonVT

If you look closely I don't think the close button animates on subsequent launches of the action mode either

JakeWharton avatar Feb 02 '12 05:02 JakeWharton

Yeah, you're right. Never noticed that.

SimonVT avatar Feb 02 '12 12:02 SimonVT

Woah, I just ran into this issue too. It's not a showstopper, but it's quite annoying.

jmgrosen avatar Feb 05 '12 05:02 jmgrosen

I think this specific bug is fixed. Subsequent action mode close buttons are clickable on all supported platforms. The problem still exists where it doesn't animate on every instance after the first on pre-3.0 which I'm pushing to 4.0.1.

JakeWharton avatar Feb 05 '12 06:02 JakeWharton

hey, I found in your latest version, the close button is still not working when actionmode is second time shown on pre-3.0 version. In your demo app, com.actionbarsherlock.sample.demos.ActionModes Activity, just clicks the Start button twice, you can find that the close button is not react to click. (on 2.3 emulator) Could you help to investigate this? Thank you.

update: I think it's NineLinearLayout's problem, because when I change it back to legacy LinearLayout, problem gone. (of cause, animation is gone :) )

chengbo avatar Jun 22 '12 06:06 chengbo

I had the same problem specifically with the "done" action being ignored (until another action button is called) on an ActionMode created after a previous ActionMode was finished. I found that by forcing the inflation of the ActionMode each time that it fixed the problem.

In the file: src/com.actionbarsherlock.internal.widget/ActionBarContextView.java Around line 203:


 public void initForMode(final ActionMode mode) {
        if (mClose == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            mClose = (NineLinearLayout)inflater.inflate(R.layout.abs__action_mode_close_item, this, false);
            addView(mClose);
        } else if (mClose.getParent() == null) {
            addView(mClose);
        }

Replace it with:


 public void initForMode(final ActionMode mode) {
//        if (mClose == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            mClose = (NineLinearLayout)inflater.inflate(R.layout.abs__action_mode_close_item, this, false);
            addView(mClose);
//        } else if (mClose.getParent() == null) {
//            addView(mClose);
//        }

Or you can remove the commented lines if you prefer cleaner code. This would have a negligible impact on performance but I would prefer a minuscule lag over a faulty done button.

lodlock avatar Jul 24 '12 16:07 lodlock

Just run into this problem too and spent several hours vainly searching my own code before searching further afield. Many thanks to lodlock for the workaround. This should be reopened at least, or preferably fixed.

andrewgoz avatar Sep 02 '12 10:09 andrewgoz

Needs re-investigation

JakeWharton avatar Sep 21 '12 05:09 JakeWharton

I'm unable to reproduce this.

SimonVT avatar Sep 21 '12 19:09 SimonVT

My project is Vespucci. Feel free to check out our code and try that. We're tracking this problem ourselves too:

http://code.google.com/p/osmeditor4android/issues/detail?id=114

It's possible the bug has morphed a little since the original report and is not "3.0+ only" anymore. ICS is OK for me, but GB is not.

andrewgoz avatar Sep 22 '12 15:09 andrewgoz

Would it be possible for you to provide a small sample that displays the behavior?

SimonVT avatar Sep 22 '12 15:09 SimonVT

I can confirm the bug, it exists in ABS 4.1 even in included demo ActionMode. Tried both in emulator 2.3.3 (ADT 20.0.3) and PocketBook A10 with Android 2.3.7 using ABS Demos project

gramotei avatar Oct 06 '12 08:10 gramotei

btw I've noticed that when I click "Close" then click Action Item, i get onActionItemClicked and then Close button gets its event:

09-26 21:23:34.711: D/ABS(1409): on action item clicked 09-26 21:23:34.711: D/ABS(1409): java.lang.Throwable 09-26 21:23:34.711: D/ABS(1409): at com.myapp.android.fragments.NewGridMainFragment$SelectionModeCallback.onActionItemClicked(NewGridMainFragment.java:149) 09-26 21:23:34.711: D/ABS(1409): at com.actionbarsherlock.internal.ActionBarSherlockCompat$ActionModeCallbackWrapper.onActionItemClicked(ActionBarSherlockCompat.java:1192) 09-26 21:23:34.711: D/ABS(1409): at com.actionbarsherlock.internal.app.ActionBarImpl$ActionModeImpl.onMenuItemSelected(ActionBarImpl.java:771) 09-26 21:23:34.711: D/ABS(1409): at com.actionbarsherlock.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:738) 09-26 21:23:34.711: D/ABS(1409): at com.actionbarsherlock.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148) 09-26 21:23:34.711: D/ABS(1409): at com.actionbarsherlock.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:879) 09-26 21:23:34.711: D/ABS(1409): at com.actionbarsherlock.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:510) 09-26 21:23:34.711: D/ABS(1409): at com.actionbarsherlock.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145) 09-26 21:23:34.711: D/ABS(1409): at android.view.View.performClick(View.java:2485) 09-26 21:23:34.711: D/ABS(1409): at android.view.View$PerformClick.run(View.java:9080) 09-26 21:23:34.711: D/ABS(1409): at android.os.Handler.handleCallback(Handler.java:587) 09-26 21:23:34.711: D/ABS(1409): at android.os.Handler.dispatchMessage(Handler.java:92) 09-26 21:23:34.711: D/ABS(1409): at android.os.Looper.loop(Looper.java:123) 09-26 21:23:34.711: D/ABS(1409): at android.app.ActivityThread.main(ActivityThread.java:3683) 09-26 21:23:34.711: D/ABS(1409): at java.lang.reflect.Method.invokeNative(Native Method) 09-26 21:23:34.711: D/ABS(1409): at java.lang.reflect.Method.invoke(Method.java:507) 09-26 21:23:34.711: D/ABS(1409): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 09-26 21:23:34.711: D/ABS(1409): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 09-26 21:23:34.711: D/ABS(1409): at dalvik.system.NativeStart.main(Native Method) 09-26 21:23:35.041: D/ABS(1409): clicked 09-26 21:23:35.041: D/ABS(1409): java.lang.Throwable 09-26 21:23:35.041: D/ABS(1409): at com.actionbarsherlock.internal.widget.ActionBarContextView$1.onClick(ActionBarContextView.java:216) 09-26 21:23:35.041: D/ABS(1409): at android.view.View.performClick(View.java:2485) 09-26 21:23:35.041: D/ABS(1409): at android.view.View$PerformClick.run(View.java:9080) 09-26 21:23:35.041: D/ABS(1409): at android.os.Handler.handleCallback(Handler.java:587) 09-26 21:23:35.041: D/ABS(1409): at android.os.Handler.dispatchMessage(Handler.java:92) 09-26 21:23:35.041: D/ABS(1409): at android.os.Looper.loop(Looper.java:123) 09-26 21:23:35.041: D/ABS(1409): at android.app.ActivityThread.main(ActivityThread.java:3683) 09-26 21:23:35.041: D/ABS(1409): at java.lang.reflect.Method.invokeNative(Native Method) 09-26 21:23:35.041: D/ABS(1409): at java.lang.reflect.Method.invoke(Method.java:507) 09-26 21:23:35.041: D/ABS(1409): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 09-26 21:23:35.041: D/ABS(1409): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 09-26 21:23:35.041: D/ABS(1409): at dalvik.system.NativeStart.main(Native Method)

gramotei avatar Oct 06 '12 09:10 gramotei

I still can't reproduce this. You're going to have to provide a full sample and instructions on how to reproduce the error.

SimonVT avatar Oct 06 '12 10:10 SimonVT

How to reproduce:

  1. Run ABS:Demos from ABS 4.1
  2. Select Action Modes
  3. Click "Start" button
  4. Click "Done" on ActionBar (finishes correctly)
  5. Click "Start" button
  6. Click "Done" on ActionBar (nothing happens)
  7. Click any action item (finishes correctly)

My emulator settings:

hw.lcd.density=120 sdcard.size=128M skin.path=800x480 skin.name=800x480 hw.cpu.arch=arm abi.type=armeabi hw.keyboard=yes vm.heapSize=24 snapshot.present=true hw.ramSize=256 image.sysdir.1=platforms/android-10/images/

gramotei avatar Oct 06 '12 11:10 gramotei

Still can't reproduce, I'm just gonna let Jake debug this one.

SimonVT avatar Oct 06 '12 11:10 SimonVT

I can reproduce it on my 2.3 device with ActionBarSherlock-Sample-Demos-4.2.0.apk, different sequence from gramotei though:

  1. Run ABS:Demos from ABS 4.2
  2. Select Action Modes
  3. Click "Start" button
  4. Click "Done" (Check Mark) on ActionBar (finishes correctly)
  5. Click "Start" button
  6. Click "Start" button
  7. Click "Done" (Check Mark) on ActionBar (nothing happens)
  8. Click any action item or "Cancel" button (finishes correctly)

XMitja avatar Oct 12 '12 16:10 XMitja

Same bug(2.3.7). Hack fix which works for me:

modeHandler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    newMode = startActionMode(newModeCallback);

                }
            }, 10);

adnaan avatar Feb 03 '13 05:02 adnaan

Same problem here.

alexjlockwood avatar Feb 10 '13 03:02 alexjlockwood

Yep this is a problem I've seen a lot, I use a similar solution to the one provided by @adnaan , which is also an effective way of stopping an ActionMode that is invoked after a 'back' button press from consuming that same 'back' button press and closing itself as soon as it's invoked o.0

coreform avatar Feb 13 '13 04:02 coreform

I'm experiencing a simliar issue on android 2.3.7. My app working perfectly in ICS. My Callback is in the Fragment not an Activity. When I click a ActionBar "Edit" MenuItem this moves the ActionBar into Action mode.

If the "Done" button is then clicked the ActionBar goes away as expected, however after done is pressed the ActionBar no longer responds to MenuItem Clicks. I can still click the back button but my "Edit" MenuItem does not detect clicks anymore.

I can still get the ActionBar back into Action mode by long holding a ListItem which triggers Action Mode. I expect the user to click my "Edit" MenuItem to change to Action Mode however after the ActionBar mode is triggered and Done pressed the MenuItems no longer work.

Any Assistance is appreciated. I have been buggering with this for a few days now and this issue seems like the closest to the problem.

I attempted suggested fixes on this page however they didn't work.

Thanks

bob9

bob9 avatar Jun 09 '13 11:06 bob9