Fragment's onOptionsItemSelected method is never called

Showing 1-5 of 5 messages
Fragment's onOptionsItemSelected method is never called Andreas Häber 6/5/11 8:45 AM
Hi,

I'm facing a problem where the onOptionsItemSelected method for a
fragment is not called. Is this a known issue/limitation? The fragment
adds the menu successfully, but it is never called to handle menu item
selection.

According to http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
:  "Note: If you added the menu item from a fragment, then the
respective onOptionsItemSelected() method is called for that fragment.
However the activity gets a chance to handle it first, so the system
calls onOptionsItemSelected() on the activity before calling the
fragment."

I've tried without adding any onOptionsItemSelected method to my
FragmentActivity. In that case nothing happens. So, I added an
override for that method and it is called. So, I tried to return both
false and super.onOptionsItemSelected but never got any call into my
fragment.

Would be great with some help with this issue.

Best regards,
Andreas Häber
Re: Fragment's onOptionsItemSelected method is never called Jake Wharton 6/6/11 6:16 AM
I've landed a potential fix for this in the dev branch of the repository on GitHub if you would like to try it right away. I am going to test it later tonight and I will release a new version (3.0.1) once it's verified to work.

Thanks for the report!
Re: Fragment's onOptionsItemSelected method is never called Andreas Häber 6/6/11 2:18 PM
Thanks for the quick fix. It works correctly for handling
onOptionsItemSelected. However, it does not make 'android:onClick'
tags in the xml for menus work. It seems like the code checks the
Activity instead of the Fragment to find the method implementation
(ref: MenuInflater.java:421). Anyhow, it's most important that we have
some mechanism to handle menu selection from fragments so it's great
to have this patch.

BR, Andreas Häber

On Jun 6, 3:16 pm, Jake Wharton <jakewhar...@gmail.com> wrote:
> I've landed a potential fix for this in the dev branch<https://github.com/JakeWharton/ActionBarSherlock/tree/dev>of the repository on GitHub if you would like to try it right away. I am
Re: Fragment's onOptionsItemSelected method is never called Jake Wharton 6/6/11 5:48 PM
Are you positive that the method defined in android:onClick will be checked for in the fragment? The documentation does not mention fragments at all, only activites. I also disassembled the compiled bytecode for Android 3.1 and do not think it checks in a context other than the parent activity.
Re: Fragment's onOptionsItemSelected method is never called Andreas Häber 6/7/11 2:34 AM
Ah, you are probably right. The documentation on this is a little
unclear so I just assumed it would work that way.

BR, andreas