I have 4 fragments but for each fragments it has different different menu options in my app with viewpager2 so the user swipe through these all fragments and when the app starts the menu option for the first fragment showing the wrong which is 4th fragment menu option item that wrongly inflated in 1st fragment whenever I open the app it only occur when I open the app freshly but when I swipe to 2nd fragment and come back to first then it work correctly but at start/opening the app firstly then it shows wrong menu item btw I using setOffScreenPageLimit(4) this problem occurs when I use this method I hope anyone could solve this problem
I guess this is my situation I tried this ->
How to correctly inflate menus for an action bar from viewpager fragments
but it shouldn't work
I am giving you here full source code for your problem. Give it a try and hope it might help you. Create an options menu according to your requirement.
Follow below steps
1 - activity_main.xml
2 - MainActivity.java
}
3 - Your viewpager2 adapter
4 - Your fragment
Output