Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Fragment's onOptionsItemSelected doesn't get called

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>
like image 805
blavi Avatar asked Jun 07 '15 08:06

blavi


2 Answers

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);
}
like image 152
Clevester Avatar answered Sep 20 '22 03:09

Clevester


You must use super.onOptionsItemSelected(item) in the parent activity's onOptionsItemSelected(...) method.

From Activity | Android Developers:

Derived classes should call through to the base class for it to perform the default menu handling.

like image 41
d3dave Avatar answered Sep 20 '22 03:09

d3dave