The last version of WhatsApp does what I'm talking about. When you scroll between tabs the toolbar shows different menu options. I can implement ViewPager with adapter and fragments but not this. How could it be done? I really don't know what kind of trick is behind. It changes everytime you switch pages
Asked
Active
Viewed 3,739 times
6
-
you have to override `onCreateOptionsMenu` on each fragment – Blackbelt Jun 04 '15 at 16:54
-
it's not something only activity can do? – user3290180 Jun 04 '15 at 17:03
-
no it is not. Fragment has also this possibility. Don't forget to call `setHasOptionsMenu(true);` in onCreate of your Fragment – Blackbelt Jun 04 '15 at 17:05
-
I don't get it, please, can you show me an example with an answers? I tried but it's not overridable – user3290180 Jun 04 '15 at 17:06
1 Answers
23
In each Fragment you have to override onCreateOptionsMenu
, with different menu/menu.xml
files
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Don't forget to call setHasOptionsMenu(true);
, in onCreate of your Fragment

Blackbelt
- 156,034
- 29
- 297
- 305
-
-
@Blackbelt can you tell me how can we make the options menu be the same in all across the view pager fragments but only tool bar menu change like in whatsApp, currently both menu are same ... – Ameen Maheen Oct 15 '15 at 04:50