Search code examples
androidcontextmenubehaviorsubmenu

Android context sub-menu opened-closed-reopened by itself


I have a context menu which include a sub-menu, when-ever I tap on the item to open the sub-menu, the sub-menu opens/closes and reopens quickly. That's very annoying but more problematic some of my users don't see the sub-menu at all, it opens/closes and that's it!

Now after experimenting I figured out that long-pressing the item actually works as soon as I release the item: the sub-menu opens properly and stays open!

So I decided to build a very basic project believing I had a bug in my app, created a new app with the wizard, a single activity, a single text on which I registerForContextMenu and a context menu with a simple sub-menu.

The issue reproduced itself immediately!!! Does anyone experience the same issue and could tell me what am I doing wrong? I believe I followed documentation and samples, but I can't find any information on this problem anywhere!!!

I've posted the issue on Google groups and reported as an issue on Android project, but so far no-one responded, here are the links to both which include the test project (not sure how I can attach a file here?).

https://code.google.com/p/android/issues/detail?id=53239&can=4&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

https://groups.google.com/forum/?fromgroups=#!topic/android-developers/SLteohmgyy0


Solution

  • To solve this, I had to get rid of any sub-menu in context menu and instead open another context menu on item selection.

    The following got rid of the flickering and ensured the sub-menu remained open. Had to use a spare hidden view to open the new context menu though.

        if (id == R.id.menu_item_for_sub_menu)
        {
            new Handler().postDelayed(new Runnable()
            {
                @Override
                public void run() 
                {
                    View v = vg.findViewById(R.id.fake_view_for_context);
                    if (v != null)
                    {
                        registerForContextMenu(v);
                        openContextMenu(v);
                        unregisterForContextMenu(v);
                    }
                }
            }, 0);
        }