2

Background

I have an app which has sub menus in the action bar, for selecting the sorting type.

It works really well if you just tap the action items.

Here's how a submenu looks like on the actionBar:

enter image description here

The code

To make it easy to understand what I did, here's a short, simple version of just the sub menu part:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.test.MainActivity" >

    <item
        android:icon="@drawable/ic_action_collections_sort_by_size_holo_dark"
        android:title="sorting"
        app:showAsAction="ifRoom">
        <menu >
            <group
                android:checkableBehavior="single"
                android:menuCategory="container" >
                <item
                    android:id="@+id/menuItem_sortByInstallTime"
                    android:title="by install time">
                </item>
                <item
                    android:id="@+id/menuItem_sortByUpdateTime"
                    android:title="by update time">
                </item>
                <item
                    android:id="@+id/menuItem_sortByAppName"
                    android:title="by app name">
                </item>
                <item
                    android:id="@+id/menuItem_sortByPackageName"
                    android:title="by package name">
                </item>
            </group>
        </menu>
    </item>

</menu>

The problem

Starting with Kitkat (Android 4.4) , instead of clicking the items, users can hold the touch and leave it when they reach the item they want to choose.

The feature is shown here , under "Drag-to-Select". Most people don't know about this feature, but people have already reported a very annoying bug about it.

In case you try out the code above (or my app), and use this feature, you will notice that you won't be able to click the same action item again, unless you click on others first.

That's right, it's blocked till you choose something else.

I've tested this issue on a Nexus 4 with Kitkat, and can confirm it.

Looking at other apps (even Google's apps), I can notice a similar issue: even though I can't find an exact example of having a grouped sub-menu, for regular sub menus, the action item is blocked for a single time. Clicking on it again will release it.

The question

Why does this occur?

Is there anything wrong with my code?

Is this a bug?

How can I fix it?

If there is no fix, should I just create my own popup menu, or is there another workaround?

adneal
  • 30,484
  • 10
  • 122
  • 151
android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

2

Is this a bug?

I can recreate it too. I checked the AOSP's issue tracker and didn't find anything, but it certinaly appears to be a bug.

Why does this occur?

I think it's related to the ListPopupWindow.ForwardingListener, but I'm not certain exactly where the problem occurs right now.

How can I fix it?

Call Activity.invalidateOptionsMenu after you select a MenuItem.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menuItem_sortByInstallTime:
            // Do something
            break;
        case R.id.menuItem_sortByUpdateTime:
            // Do something
            break;
        case R.id.menuItem_sortByAppName:
            // Do something
            break;
        case R.id.menuItem_sortByPackageName:
            // Do something
            break;
        default:
            break;
    }
    invalidateOptionsMenu();
    return true;
}

Limitations

Unfortunately, it looks like this solution is only viable for menu items not placed in the action bar's overflow menu. If you'd like to follow further updates regarding this problem, you should refer to issue #69205 on the AOSP's issue tracker.

adneal
  • 30,484
  • 10
  • 122
  • 151
  • This works, but now I have to also re-update the action items to match the current state. Wonder if there is another way. Anyway, Thank you. BTW, I've posted a bug report here: https://code.google.com/p/android/issues/detail?id=69205 – android developer May 02 '14 at 06:50
  • I think this should also be done for any kind of action item, as some of them might be in the overflow action item menu. – android developer May 02 '14 at 07:27
  • Sadly this solution only works for action items that are not on the overflow action item. for those that are on the overflow action item, the second issue (of Google's apps) appear. Do you know how to fix this issue? – android developer May 02 '14 at 07:45
  • @androiddeveloper Unfortunately, if we're right about this being a bug, and I think we are, I don't see there being a better workaround for now. There's just no way to modify the behavior of the [`ActionMenuPresenter`](https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/view/menu/ActionMenuPresenter.java), even through reflection. – adneal May 02 '14 at 08:29
  • 1
    OK, I will tick your answer if you edit it , so that people will know this only works for the cases I've written. I really hope Google will fix this issue, even if it's minor. – android developer May 02 '14 at 08:47
  • @androiddeveloper If you're that satisfied with the edit I made two days ago, you should consider editing my post yourself so that anyone else having trouble understands the limitations as well as you would like them to. – adneal May 04 '14 at 12:47
  • I don't get notified when someone edits an existing post, so now that you've posted a comment saying you've edited it, I will mark it. – android developer May 04 '14 at 13:00
  • @androiddeveloper In the future, you might consider going ahead and editing the post yourself, rather than relying on the user to notify you. If they forget or assume you'd check back, someone else may end up misinformed. Otherwise you're essentially holding the answer at ransom, which doesn't really do any good. – adneal May 04 '14 at 13:09
  • I prefer to never edit another person's posts. I consider this rude and annoying even if it's for handling typos . The original person who writes the answer should be in charge of what he has written. not anyone else. Also, when the person itself is doing it, he might learn from it something. otherwise, I will feel like this guy: http://xkcd.com/386/ – android developer May 04 '14 at 13:45