I need to add a options menu for each fragment in bottom navigation view. currently each app bar has only the name of the fragment. I want to add specific options menu for each.
Asked
Active
Viewed 56 times
0
-
You no need to create appbar for every fragments... Try this https://stackoverflow.com/questions/8308695/how-to-add-options-menu-to-fragment-in-android#:~:text=Add%20setHasOptionsMenu(true)%20in%20the,items%20in%20your%20Fragment%20class.&text=You%20don't%20need%20to,onPrepareOptionsMenu%20method%20available%20in%20Fragment.&text=Show%20activity%20on%20this%20post.,-In%20my%20case – Gobu CSG Jul 10 '22 at 07:08
-
@GobuCSG the code there seems to be deprecated. Is there a latest version. – UVic Jul 10 '22 at 14:49
-
It's not deprecated... Share those code here... – Gobu CSG Jul 10 '22 at 17:55
-
Yes it is .. Now you need to use menuProvider in each fragment . – UVic Jul 11 '22 at 05:29
-
@GobuCSG I have answered the question myself.. check it out. – UVic Jul 11 '22 at 05:32
1 Answers
0
Found solution [here]: 'setHasOptionsMenu(Boolean): Unit' is deprecated. Deprecated in Java
one thing I would like to add is so that the menu is removed when going to another fragment.
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
requireActivity().removeMenuProvider(menuProvider);
}

UVic
- 1,671
- 3
- 14
- 21