OnOptionsItemSelected not called from the detail view of the fragment wizard

I have a representation of the main part, with both basic and detailed fragments with my menu items on the action bar; the wizard has some, and there are some in the details, but the item data item element does not call onOptionsItemSelected when clicked. This problem is on the tablet.

On the other hand, if the same code is run on the telephone emulator, the elements of the action bar of the detailed view work without problems.

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> 

The supercharger works fine, and both fragments in the main view of the part have setHasOptionsMenu(true); in their onCreate method.

EDIT

onCreateOptionsMenu in fragment activity

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

onCreateOptionsMenu in the main snippet

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

onCreateOptionsMenu in 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"); } 
+4
source share
1 answer

I got a solution from this link.

It says that we can achieve the desired behavior without intercepting the same event in the fragment activity (parent activity of the fragment).

This link shows two ways, but I do it using the following in a FragmentActivity

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

Give it a try.

+2
source

Source: https://habr.com/ru/post/1493657/


All Articles