Objective
Nested level Two fragment cannot receive Oncreateoptionsmenu event, set Sethasoptionsmenu also no use.
Body
Supplementary Note:
If you manually call level two fragment through the cache fragment, there may be some puzzling problems, such as more menu not displayed.
Solution:
Add menu to the first level fragment, you can manually call level two of this method in the first level onoptionsitemselected to handle related events.
Sample code:
@Override public
void Oncreateoptionsmenu (Menu menu, Menuinflater inflater) {
switch (getselectedposition ()) { Case
1:
inflater.inflate (r.menu.menu1, menu);
break;
Case 2:
inflater.inflate (R.MENU.MENU2, menu);
break;
Case 0:
default:
inflater.inflate (R.MENU.MENU3, menu);
break;
}
}
@Override Public
Boolean onoptionsitemselected (MenuItem item) {
Fragment Fragment = getfragmentbyposition ( Getselectedposition ());
if (fragment!= null)
fragment.onoptionsitemselected (item);
return super.onoptionsitemselected (item);
}
Code Description:
The sample code is a level fragment,getfragmentbyposition that returns a level two fragment.
That's what Android solves fragment's inability to receive oncreateoptionsmenu events, hoping to help friends of Android developers solve similar problems.