I have a fragment class that extends Fragment
and calls setHasOptionsMenu
to participate in the menu. This class also implements onCreateOptionsMenu
, onPrepareOptionsMenu
and onOptionsItemSelected
.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
....
}
I'm dynamically loading this fragment using a FragmentTransaction
in my Activity (that extends FragmentActivity
).
However none of the menu callbacks (onCreateOptionsMenu
, onPrepareOptionsMenu
and onOptionsItemSelected
) are being called (I've debugged with some breakpoints in those methods) and the menu isn't shown.
Am I missing something? Do I need to add something in my Activity?
I'm using the Android Compatibility Library, compiling with L11 SDK and testing in a Xoom.
EDIT: I've found the problem. My AndroidManifest is targeting L11, this seems to hide the menu button and prevent from the callbacks being called. However if I remove this from the manifest I loose some other features I need (for example the activated state in lists). Does anyone know how to solve this issue (enable the menu button) without removing the targetSdkVersion=11
from the Manifest?
I had same problem and solution that worked for me is:
Remove or comment any onOptionsItemSelected() ,onMenuItemSelected() even onPrepareOptionMenu() and leave in Activity onCreateOptionsMenu() only:
In Fragment class, in onCreateView(), put:
In Fragment class add :
Tested and worked on Android 4.4
I had the same problem, but i think its better to summarize and introduce the last step to get it working:
Add setHasOptionsMenu(true) method in your Fragment's
onCreate(Bundle savedInstanceState)
method.Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)
(if you want to do something different in your Fragment's menu) andonOptionsItemSelected(MenuItem item)
methods in your Fragment.Inside your
onOptionsItemSelected(MenuItem item)
Activity's method, make sure you return false when the menu item action would be implemented inonOptionsItemSelected(MenuItem item)
Fragment's method.An example:
Activity
Fragment
I hope this will be helpful.
Cheers.
If you're having this problem with ActionBarSherlock, you need to make sure your Fragments are SherlockFragments, not mere SupportFragments, and that what you're overriding is
NOT
If you do the latter, you should get some sort of warning about the function being final and you being unable to override it. This is a warning that you're trying to override the wrong function!
If you fix the error by switching the class from SherlockFragment to a mere Fragment, you can create the function . . . but it won't get called.
I had this problem when I was using the ViewPagerIndicator in conjunction with ActionBarSherlock. Although it appeared this was fixed I still ran into the problem. The work around I found was to call into the fragment manually.
Another possible case is when you use a common id for a common action in each fragment; for instance R.id.action_add
Today I had such situation: hitting the option menu [add] was invoked the "wrong"
onOptionItemSelected
because each fragment (replaced dynamically using aDrawerLayout
) had the sameR.id.action_add
.Short story, if you have such situation always check that your fragment is visible:
if (!isVisible()) return false;
Long story, pay attention at the
onOptionItemSelected
chain!If you add your fragments with (something like) this:
and you have defined the same id for a common action (let's say R.id.action_add) in each fragment; don't forget to add this line to each: if (!isVisible()) return false;
Aromero, Don't forget to override the onCreateOptionsMenu using the fragment version of the method, similar to this:
This goes in the fragment, by the way, and adds to the inflated menu of the Activity, if there is one. Had the same problem myself, until I figured this out.
Kim