Fragment's onOptionsItemSelected method is never called

5,200 views
Skip to first unread message

Andreas Häber

unread,
Jun 5, 2011, 11:45:40 AM6/5/11
to jakewharton-projects
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

Jake Wharton

unread,
Jun 6, 2011, 9:16:35 AM6/6/11
to jakewharto...@googlegroups.com
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!

Andreas Häber

unread,
Jun 6, 2011, 5:18:05 PM6/6/11
to jakewharton-projects
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

Jake Wharton

unread,
Jun 6, 2011, 8:48:53 PM6/6/11
to jakewharto...@googlegroups.com
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.

Andreas Häber

unread,
Jun 7, 2011, 5:34:11 AM6/7/11
to jakewharton-projects
Ah, you are probably right. The documentation on this is a little
unclear so I just assumed it would work that way.

BR, andreas

Sonali Kale

unread,
Dec 23, 2019, 11:57:02 PM12/23/19
to ActionBarSherlock
I got solution for this without using any library 
i.e 

Activity code 

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;

default:
// with below code fragment on option selected method gets called
if(fragment != null)
fragment.onOptionsItemSelected(item);
}
return true;
}

Fragment Code 

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Do something that differs the Activity's menu here
//MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.wifinity_setting, menu);
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
SpannableString spanString = new SpannableString(menu.getItem(i).getTitle().toString());
spanString.setSpan(new ForegroundColorSpan(Color.BLACK), 0, spanString.length(), 0); //fix the color to white
item.setTitle(spanString);
}
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.add_menu:
Intent intent3 = new Intent(context, Activity.class);
startActivity(intent3);
return true;

}
return true;
}
Reply all
Reply to author
Forward
0 new messages