Android: Fragment’s onOptionsItemSelected doesn’t get called

androidandroid-actionbaractivityandroid-fragmentsandroid-menu

I have an ActionBarActivity and one Fragment. The Activity has no menu inflated, while the Fragment has a menu with two buttons. Fragment menu is visible, but the buttons don't react at all when tapped. At debugging I can see that both onCreateOptionsMenu() for Fragment and Activity get called, but when tapping buttons no onOptionsItemSelected() gets called, neither from Activity nor from Fragment.

Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return false;
}

Fragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mainActivity = (NavigationActivity)getActivity();

    setHasOptionsMenu(true);
}

 @Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState){
    return (ScrollView) inflater.inflate(R.layout.tutoring_detail, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.query_details_menu, menu);

    super.onCreateOptionsMenu(menu, inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.accept_query:
            respondToQuery(true);
            return true;
        case R.id.decline_query:
            respondToQuery(false);
            return true;
        default:
            break;
    }

    return false;
}

Menu to be displayed in Fragment

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

<item
    android:id="@+id/accept_query"
    android:orderInCategory="100"
    app:showAsAction="always"
    android:checkable="true"
    style="?android:attr/borderlessButtonStyle"
    app:actionViewClass="android.widget.ImageButton"/>

<item
    android:id="@+id/decline_query"
    android:orderInCategory="101"
    app:showAsAction="always"
    android:checkable="true"
    style="?android:attr/borderlessButtonStyle"
    app:actionViewClass="android.widget.ImageButton"/>
</menu>

Best Solution

In the Activity class,

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);   
                return false;
}

In the Fragment,

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    super.onCreateOptionsMenu(menu, inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Toast.makeText(getActivity(), "called " + item.getItemId(), Toast.LENGTH_SHORT).show();
    return super.onOptionsItemSelected(item);
}