15

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.

senior
  • 435
  • 5
  • 13

2 Answers2

16

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

k4dima
  • 6,070
  • 5
  • 41
  • 39
  • Are you sure about that? I am using ActionBarSherlock but as far as I know ABS just uses normal ActionBar when the device is above ICS. I am using nested fragments and support revision 11 but the menu is not created. – tasomaniac Feb 19 '13 at 12:05
  • @tasomaniac Yes, i am sure. And you always can look in the source of ABS and android, as i did. Problem is not in actionbar, problem in the way ABS handles calls to onCreateOptionsPanel() from FragmentActivity to fragments and not to nested fragments. – k4dima Feb 20 '13 at 22:28
  • You are saying that if I not use ActionBarSherlock and compile it with Android 4.0 and use support library I can use onCreateOptionsMenu in nested fragments? – tasomaniac Feb 20 '13 at 22:34
  • @tasomaniac Yes, and not only from android version 4.0, support library runs on android 1.6 and up. And you also can use onCreateOptionsMenu() with ActionBarSherlock as i wrote in my answer. – k4dima Feb 20 '13 at 22:59
  • Ok that makes sense. I was calling it manually. Hope Jake Wharthon fix this in the next update. – tasomaniac Feb 21 '13 at 01:50
  • Thanks for your input, this saved me quite some time. – Bogdan Zurac Feb 22 '13 at 15:39
  • @kreker this issue solved or not? or any alternative solution available? I'm getting this same issue SherlockActivity(with navigation bar) ----> SherlockFragmentTabstrip (withpagerAdapter) -->SherlockFragment(child fragment that I failed to call Option-menu) – LOG_TAG Oct 09 '13 at 09:44
  • @kreker Thnx! can you explain how to call child fragment's onCreateOptionsMenu() manually from parent fragment? like this? getChildFragmentManager().onCreateOptionsMenu() in parent fragment? it is not working! looks like I have migrate from ABS to ABC? – LOG_TAG Oct 10 '13 at 05:18
  • @LOG_TAG well, find them somehow in code... I didn't use ABC. – k4dima Oct 10 '13 at 17:10
11

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • hi CommonsWare, thanks for your fast replay, that's what i ended up doing, but i was hoping for a more encapsulated solution. – senior Dec 09 '12 at 13:19
  • Yes..!! i got stuck on this issue too.. and i did what mark murphy has told.. Thanks mark – Sandeep Dhull Jun 10 '13 at 16:10
  • may I know how is your workaround working? I tried everything and it's still messed up.. – Zyoo Sep 18 '13 at 05:12