Eriz
Eriz

Reputation: 972

onOptionsItemSelected not being called from fragment master detail view

I have a master detail view, with both the master and detail fragments with their own action bar menu items; the master has some and the detail has some, but the details action bar item don't call onOptionsItemSelected when clicked. This problem is on tablet.

On the other hand if the same code is run on a phone emulator the action bar items of the detail view work with no problem.

menu.xml

<item
    android:id="@+id/save_menu"
    android:icon="@drawable/ic_checkmark_holo_light"
    android:showAsAction="always|withText"
    android:title="Save">
</item>
<item
    android:id="@+id/cancel_menu"
    android:icon="@drawable/ic_menu_close_clear_cancel"
    android:showAsAction="always|withText"
    android:title="Cancel">
</item>

inflater is working fine and both the fragments in master detail view have the setHasOptionsMenu(true); in their onCreate method.

EDIT

onCreateOptionsMenu in the fragment activity

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

onCreateOptionsMenu in the master fragment

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

onCreateOptionsMenu in the detail fragment

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.addfragment_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
    System.out.println("onCreateOptionsMenu called");
}

Upvotes: 4

Views: 6929

Answers (1)

kaushal trivedi
kaushal trivedi

Reputation: 3443

I got the solution from this link.

It says that we can achieve the desired behaviour by not intercepting the same event in Fragment Activity (Parent Activity of Fragment).

This link shows two ways however i get my thing done by using following in FragmentActivity

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        return false;
    }

Try it out.

Upvotes: 2

Related Questions