0

I have the following situation: an Activity that contains a Fragment which has a ViewPager that has Fragments:

                                 -> Childfragment
Activity -> Fragment w ViewPager -> Childfragment
                                 -> Childfragment

I use a FragmentStatePagerAdapter on the ViewPager.

The problem is: I have to switch ON and OFF the ActionBarMenu of the Childfragments, my approach was (in short) the following in my host Fragment:

boolean childsHaveMenu = false; 

public void setChildsHaveMenu(boolean childsHaveMenu) {

    this.childsHaveMenu = childsHaveMenu;
    pagerAdapter.notifyDataSetChanged();
    getActivity().supportInvalidateOptionsMenu();

}

and in my FragmentStatePagerAdapter's getItem() method:

        fragment.setHasOptionsMenu(childsHaveMenu);
        return fragment;

Unfortunately, pagerAdapter.notifyDataSetChanged(); and getActivity().supportInvalidateOptionsMenu(); both don't seem to force the Childfragments to 'update' it's ActionBarMenu, at least not consistently.

My second approach was to send an event to the Exposes with a boolean to tell them of they should show their ActionItems and then hiding or showing them in onCreateOptionsMenu(), but again I can't force the Childfragments to call onCreateOptionsMenu() so the ActionBar updates.

Any ideas on how I could solve this would be highly appreciated :)

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • You should inflate the menu items in the Fragments' onCreateOptionsMenu() callbacks and you should call setHasOptionMenu() in all the Fragments. – tasomaniac Nov 12 '13 at 09:32
  • And how would I hide the Menu again when I need to? – fweigl Nov 12 '13 at 09:34
  • When you create the menu from the appropriate fragment and use viewpager the menu items will automatically be hidden. FragmentStatePagerAdapter automatically hides the respective menu items when the page is hidden. You need the inflate the menu items not from the Activity but in the Fragments. – tasomaniac Nov 12 '13 at 09:38
  • But I also need to hide menu of the currently shown Fragment from time to time, that's the problem. – fweigl Nov 12 '13 at 09:47
  • Ok then make all the menu items private global variables in your Fragments and make a public function named hideMenuItems() and you can call hide() function of all the items there. You can call this hideMenuItems() function both in this Fragment and in your Activity. – tasomaniac Nov 12 '13 at 09:57
  • Hello, are you still working on it or do you find a solution? If you have no solution, I have done a set-up with a view pager holding my fragments. The 3rd page hold fragment with a "switch between 2 fragments" option. I successfully manage after much efforts to have a logical and nice behavior for my item in action bar : appearing, updating, etc. So if you need. If solved, please indicate a solution, I am interested. – Poutrathor Nov 19 '13 at 15:11
  • @Poutrathor I ended up managing the menu from the Fragment with Viewpager (the middle fragment), invalidateOptionsMenu() on every page change of the ViewPager and getting all the information I need from the child fragments with the help of this answer: http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager – fweigl Nov 20 '13 at 08:38
  • good. Personally i did not need to use invalidateOPtionMenu() but I think you need it because of the wraping fragment around the viewpager, right? – Poutrathor Nov 20 '13 at 09:11
  • Exactly, the fragment that 'contains' the menu doesn't change, so I need to invalidate when the ViewPagers content is changed. – fweigl Nov 20 '13 at 09:21

0 Answers0