OptionsMenu of nested fragments in ViewPager

I am using ActionBarSherlock and I am trying to implement the structure of nested fragments using the viewpager.

I have an activity that contains some views and a wrapper fragment (FragmentA)

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

By default, onCreateOptionsMenu events are not dispatched to child fragments, as discussed here . Therefore, I use this solution to solve the problem.

It works great on API level 17, but below it does not show the parameters for the first fragment, but when I look at the others, everything starts to work fine. I tried calling onCreateOptionsMenu from the parent fragment, but did not get the result. It also works when I scroll back to the first fragment.

Any suggestions?

Update:

A clearer way to express structure:

By wrapper snippet, I meant the snippet that contains the viewpager. Thus, the structure

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) 

I also found a workaround that 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); } 
+6
source share
2 answers

You are likely to initialize your view player before the activity is completed.
This is a problem because child fragments create their own options menu, but then activity cancels all options menus.
You must initialize your pager inside the onActivityCreated method of your wrapper fragment.

+3
source

Edit

 if(viewPagerAdapter.getItem(view_pager.getCurrentItem()) instanceof FragmentToFind) { FragmentToFind fragment = (FragmentToFind) viewPagerAdapter.getItem(view_pager.getCurrentItem()); fragment.onCreateOptionsMenu(menu, inflater); } 
+1
source

Source: https://habr.com/ru/post/957491/


All Articles