Fragment Option Menu in FragmentPagerAdapter - android

I am using a Custom FragmentPagerAdapter for SwipeViews in ActionBar.NAVIGATION_MODE_TABS mode.
I want the Fragments inside the adapter to provide their custom Menus. But I observed that onCreateOptionsMenu() method inside the Fragment is not getting called even when I used setHasOptionsMenu(true);
inside the onCreate() callback of the Fragment.
In short,How can I get custom Options Menu for each Fragment in ViewPager?

Please write the code in onCreateOptionsMenu & onPrepareOptionsMenu of your activity to inflate the from menu xml. You can then customize the menu in your fragment by using these functions inside your fragment again. I tried it and it worked for me.

Actually your problem seems to lie in ViewPager. Either its not instantiated properly or its Visibility is set to Visibility.GONE.
Make sure that ViewPager is Visible on screen by default or there is no code which changes its visibility. Get back to me if problem persists.

Related

Menu item not appearing in the fragment

I am trying to put menu item on the single fragment using the code in the photos but the menu not appearing
You are adding your MenuProvider in onCreateView. According to the official documentation. You should add it after the Fragment's view is created, inside the fragment's onViewCreated callback.

Swap toolbar on fragment launch

I have an activity with a NavigationDrawer and a full screen fragment. Clicking on various items in the NavigationDrawer inflates a different fragment in the activity.
I would like to also swap in a different toolbar when launching a different fragment. The reason I want to do that instead of inflating a new menu is that the toolbar I want to swap in is a bit complicated and has things like an EditText in it.
Is there some way to do this either in the activity before inflating a fragment or maybe in the fragment?
Just include your Fragment specific toolbar in your Fragment's layout so that it's visible in your Fragment permanently.
Now in the onStart() method of your Fragment -
getActivity().getSupportActionBar().hide();
This will hide the activity's default toolbar so that it does not overlap with the Fragment's toolbar.
Then again in the onStop() method of your Fragment -
getActivity().getSupportActionBar().show();
So that the Activity's toolbar is visible again outside that Fragment.
each time you call a fragment use "invalidateOptionsMenu" and using fragment id you can set visibility of menuitems in toolbar... if that is what u need... hope it helps

onCreateOptionsMenu of Fragment called twice

Scenario:
I using a ViewPager inside a Fragment. This ViewPager inflates 3 fragments (AFragment, BFragment, CFragment) pages using FragmentPagerAdapter.
I have printed logs inside
onCreateOptionsMenu
onPrepareOptionsMenu
of all 3 fragments.
Problem:
For AFragment when created onCreateOptionsMenu was called only once,
but when I swiped to BFragment onCreateOptionsMenu and
onPrepareOptionsMenu for BFragment were called twice. And similar
thing happened with CFragment.
Can anyone explain why this is happening and How I can avoid it?
Thanks.
Because android by defaults loads a next and previous fragment in background, in order to load fragments efficiently and to remove lag.
However you can customise the same by using setOffscreenPageLimit (int limit), Which is used to set the number of extra fragments you would like to load in background. By default its set to 1.
Read Android fragments strange load for more information

Android- How to recreate a CustomView in ActionBar?

I have a activity that implements a CustomView to inflate the ActionBar with a Checkbox.
This Checkbox is a "LikeButton" to a article from a source and when the user swipe from left/right it load another articles from the same source.
The code is working fine for the first article that the user open, but when he swipe the Checkbox stays with the same state from the firt article.
How can I build a CustomView diferent for every article and inflate it when the user swipe?
Use a ViewPager with Fragments containing your custom views. Also, for each fragment, make sure that you enable it's own options menu by calling Fragment.setHasOptionsMenu(boolean hasMenu);. Once your do this, just Override your optionsMenu methods within your fragment classes.

Hide/show ViewPager in Android

I have this app structure:
MainActivity with a global menu and Fragment container.
First Fragment: a ViewPager with three pages.
Second Fragment: a list
I insert the Fragments on the OnCreate() in the MainActivity and change their visibility with show and hide (using the FragmentTransaction).
When I launch the app the first Fragment appear in the screen without problem, when change to the second Fragment also work like a charm but when I come back to the first Fragment the app doesn't show anything inside the ViewPager.
Are there any particular option that I have to change in the ViewPager to solve this?
The problem seems due to that android does not allow to have nested fragments. In my case I can manage the situation using simple views instead of fragments inside the ViewPager.
I used ViewStub to show/hide ViewPager.
The basic idea was you created ViewStub in layout file, and inflate/setVisible while necessary.
The only trick was, I had to setAdatper to null otherwise it crashed while re-createView when off and on screen.
#Override
public void onDestroyView() {
super.onDestroyView();
viewPager.setAdatper(null);
}

Categories

Resources