6

I am using ActionBarSherlock and I am trying to implement a nested fragment structure with a viewpager.

I have an activity which contains some views and a wrapper fragmet (FragmentA)

This FragmentA contains a view pager which shows FragmentA.1, FragmentA.2 ,FragmentA.3.

By Default, onCreateOptionsMenu events are not dispatched to child fragments as it is discussed here. So I am using this solution to overcome the issue.

It works great over API level 17, but for below it does not show the optionsmenu for the first fragment but when i scroll to others everything starts to work just fine. I have tried calling onCreateOptionsMenu from parent fragment but no result. It also works when i scroll back to first fragment.

Any suggestions?

Update :

More clear way of expressing the structure :

By wrapper fragment, i meant the fragment which holds the viewpager. So the structure is

ACTIVITY 
        -> WRAPPER FRAGMENT (holds viewpager and passes childfragmentmanager to adapter(FragmentPagerAdapter) as fragmentmanager) (parent is activity)
             -> CHILDFRAGMENTS(items of viewpager) (parent is wrapper fragment but viewpager manages its framelayout)

Also i have found a temp solution which is not so nice :

if(Build.VERSION.SDK_INT > 17){

            pager.setCurrentItem(1,false);

        } else {

            new android.os.Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    pager.setCurrentItem(1, true);
                }
            }, 300);


        }
bugraoral
  • 2,630
  • 1
  • 21
  • 25

2 Answers2

3

Probably you initialise your view pager before the end of the activity's creation.
This is the problem because child fragments create their options menu but then, activity invalidate all options menu.
You must initialise your pager inside onActivityCreated method of your wrapper fragment.

Robert
  • 5,278
  • 43
  • 65
  • 115
bkrcinar
  • 345
  • 1
  • 7
1

Edit

if(viewPagerAdapter.getItem(view_pager.getCurrentItem()) instanceof FragmentToFind)
{
    FragmentToFind fragment = (FragmentToFind) viewPagerAdapter.getItem(view_pager.getCurrentItem());

    fragment.onCreateOptionsMenu(menu, inflater);
}
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
  • thanks for the effort but getChildFragmentManager() returns null when called in onCreateOptionsMenu. Also how can i get the id of the current fragment in viewpager? – bugraoral Nov 25 '13 at 15:39
  • I changed my code, because I think your problem has to do with the fragment navigation. Hope it helps. – Kevin van Mierlo Nov 25 '13 at 16:24
  • You are adding a fragment inside viewpager item(a fragment). This adds one more ,unnecessary, level of fragment. I have updated the question may be i didn't explain the question clear enough. – bugraoral Nov 26 '13 at 08:49
  • I don't know if this matters, but the wrapper fragment where you hold the view pager in, should be `SherlockFragmentActivity` to host the view pager and the fragments. The first time I tried using onCreateOptionsMenu on the items that were in the view pager it just worked like this: put `setHasOptionsMenu(true);` in the onCreate(). Then the fragment will call onCreateOptionsMenu() which will be like this in a fragment: `@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)` – Kevin van Mierlo Nov 26 '13 at 10:26
  • Then you use the inflater if you have objects in your menu xml or `super.onCreateOptionsMenu(menu, inflater);` if you want to add them programatically – Kevin van Mierlo Nov 26 '13 at 10:28
  • Well, challenge is to use a fragment to hold a viewpager properly :) Of course i would have used a fragmentactivity if i could and didn't need get into all these troubles. But this "screen" is a part of a flow and i pass a great amount of data to and from this screen. And i am using the activity to manage the flow. So in every other way, this usage is way healthier for me. If you could answer answer the question, i will be happy to give you a bounty. – bugraoral Nov 26 '13 at 17:31
  • I changed my answer. I think this is what you need (see Edit in my answer) – Kevin van Mierlo Nov 27 '13 at 08:59
  • im am sorry, this may be an answer but does not seem to as a legit one. I was looking for a hack in 'FragmentPagerAdapter' or 'Watson.java'. Having adapter added the fragment int the layout and then adding via getFragmentManager().beginTransaction().replace(R.id.container, navigateToFrag).addToBackStack(null).commit(); doesn't look like an actual solution for me. – bugraoral Dec 03 '13 at 13:22
  • Sorry my answer was too big, I changed my answer cause this was the part that should work. This should work. Let me know – Kevin van Mierlo Dec 03 '13 at 13:44