Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - NestedFragments participate in populating the options menu

i am implementing the new nested fragment feature and had stumble into a problem. my view is basically this: a main activity(A) that includes a fragment(B), this fragment(B) includes a pager adapter that has 3 pages each of them is a fragment(C) also. previously to the new getchildfragmentmanger this was not doable, but it works perfectly. but one issue did arise, i want fragments C to be able to participate in populating the option menu. i tired putting setHasOptionsMenu(true) in the onActivityCreated method on each of my C fragments and overriding also onCreateOptionsMenu but nothing happens... if i try to populate the menu from fragment B (which is the container of the pager adapter) i can change the menu items...

any thoughts ?

Thanks.

like image 480
senior Avatar asked Dec 09 '12 12:12

senior


2 Answers

Yes they can.

With android 4.2 or support library revisiion 11 nested fragments participate in populating options menu, as allways you need to call setHasOptionsMenu(true) during onCreate().

But if you are using ActionBarSherlock they won't, you have to manually call from parent fragment onCreateOptionsMenu() nested fragment method onCreateOptionsMenu().

Update: issue

like image 186
kreker Avatar answered Oct 22 '22 21:10

kreker


From my read of the source code, it appears that the implementations of FragmentManager and Activity only work with the root FragmentManager for adding to the options menu/action bar, not and child FragmentManager instances.

Fragment B presumably will need to manage the options menu/action bar on behalf of the contents of the ViewPager, changing what is in the options menu/action bar based upon the pages being shown and hidden in the pager.

like image 38
CommonsWare Avatar answered Oct 22 '22 23:10

CommonsWare