Menu item not appearing in the fragment - android

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.

Related

Android Menu duplication fixed with clear but it clears the parent activity menu

I have created different fragment and populated some menu based on the fragment. I was getting duplicate menu items so I used clear as suggested various stackoveflow posts but the problem is when I use clear in fragment it also clears the main activity menu items.
Activity and all fragments inside it use the same Menu instance. So no matter where you call clear() method, all items will be deleted.
Menu items that are used across all child fragments should be created inside Activity. Fragment-specific options should be created inside corresponding fragment. Ideally you should not have duplicated menu items. If two fragments have same menu item but third doesn't -- prefer create menu item inside every fragment but not single menu item inside activity. With this approach you will not get duplicates.
But If you want just to delete duplicates you can use menu.removeItem(itemId) method. Also you can hide redundant item with menuItem.setVisible(false).
When I switch between Fragments, I get duplicated menu
Maybe you have two active fragments at the same time. It can be when you use add method instead replace.

Should a fragment be able to change the State of a View inside the host Activity?

If I need to hide a view inside the Activity when a certain fragment is inflated, is it ok to let the Fragment do the State Change?
For example I have a three Fragments (FragmentA, FragmentB, FragmentC) and one Activty. The Activity have a BottomNavigation View but its visibility should be set to Gone if FragmentB is inflated inside the Activity.
If I placed the managing of the BottomNavigation Visibility inside the fragment then, I am sure that whenever that fragment is inflated the view will certainly be set to gone.
My only problem is that, if there come a time that I need to reuse that fragment and show the BottomNavigation at the same time. I wont be able to do so because the Fragment will automatically set the Visibility of the BottomNavigation to Gone.
Can anyone give me some tips? Thanks in advance.
In your case, do not control the visibility of the BottomNavigation inside the Fragment, do it inside the Activity with a callback.
read about callback this in part "Creating event callbacks to the activity"
Fragments should be self-sufficient and should not know anything about other Fragments and Activites.

In Action Bar set custom view within fragment

I have an Activity that holds some fragments.
This Activity is associated with a view pager, this view pager uses FragmentPagerAdapter, so every page of view pager is treated as a fragment.
Now, suppose I have customize the action bar view in any one of the fragment, and that custom view could be seen in other fragments also.
getActivity().getSupportActionBar().setCustomView(R.layout.custom_view_home);
getActivity().getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
This is because, we are customising the view, using the activity context.
My Question :
Q. Cant we set the Custom View of action bar within a fragment ? So, it would not get reflected to other fragment.
Short answer: Yes.
You should only allow currently visible fragment to add custom view to the ActionBar. Of course, you can do it right from the Fragment, with whichever context.
Requesting invalidation of the options menu will remove current custom view with the new one, i.e invalidating entire ActionBar. Similar approach you can use from the link above.

Fragment Option Menu in FragmentPagerAdapter

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.

Android: Master/Detail Flow on tablet

I've created application using master detail flow. I have custom ListView and FrameLayout as container to show detail of selected items from ListView. Everythig's fine but my detail is shown after onClick on ListView item. I would like to show detail of first item in ListView automatically after activity is created (sth. like android Settings activity). How should I do that? Thank you for response and your help.
Put your Fragment directly on onCreate() on the FrameLayout.
FragmentActivity frag = new FragmentActivity();
getSupportFragmentManager().beginTransaction().add(frag, details).commit();
and than always replace() via FragmentManager...

Categories

Resources