Before I ask the question let me give some additional info:
I have a Fragment which adds a MenuItem
in the onCreateOptionsMenu
. The added MenuItem also has a custom ActionProvider
added to it.
The custom ActionProvider
doesn't use a ActionView but instead prepares a SubMenu
with certain items to choose from. For this reason I've setup the ActionProvider
as follows:
- I return
null
in theonCreateActionView
method hasSubMenu()
returnstrue
- In
onPrepareSubMenu(SubMenu Menu)
I first clear the current menu, afterwards add the needed items - I correctly handle the
onMenuItemClick
in the ActionProvider - Since I'm using ActionBarSherlock, my ActionProvider extends
com.actionbarsherlock.view.ActionProvider
rather thenandroid.support.v4.view.ActionProvider
(don't know if it should make any difference, but at this point I don't know what does)
This all goes well on devices with Android versions higher then 3.0. I see the added MenuItem
, it has the correct SubMenu
(from the bound ActionProvider
) and the correct actions take place for each menu option. But with devices running Android versions below 3.0 (I could only test this on a device running 2.3.6) something weird happens; hence the following question.
Google clearly states:
"onPerformDefaultAction()
The system calls this when the menu item is selected from the action overflow and the action provider should perform a default action for the menu item. However, if your action provider provides a submenu, through the onPrepareSubMenu() callback, then the submenu appears even when the action provider is placed in the action overflow. Thus, onPerformDefaultAction() is never called when there is a submenu."
Taken from: http://developer.android.com/guide/topics/ui/actionbar.html#CreatingActionProvider
From the excerpt I take it onPerformDefaultAction()
should NEVER be called in my custom ActionProvider. Yet on devices running Android version 2.3.6 the onPerformDefaultAction()
DOES get called, which also prevents the SubMenu
from showing.
My question is; why does the onPerformDefaultAction()
gets called instead of the onPrepareSubMenu(SubMenu Menu)
? I need a submenu on devices running Android 2.3.6 as well..
EDIT:
I managed to fix my problem using the same technique from the SubMenus.java
from the ActionbarSherlock demo-code. This involves adding a SubMenu
instead of a custom ActionProvider to the menu in onCreateOptionsMenu(Menu menu)
, and attaching OnMenuItemClickListener
to each MenuItem there.
The workaround is nice and simple. Still, this does not answer my question as to why custom ActionProviders do not work.