Skip to content Skip to sidebar Skip to footer

Implement A Different Action Bar Item In Each Fragment Of Navigation Drawer

I have a Navigation drawer with fragments. In all the fragments it shows a button item in main.xml. Now, In some fragments I want to implement searchview. For that I made search.xm

Solution 1:

For every fragment that needs to have a menu:

  • In the constructor call this.setHasOptionsMenu(true).
  • Implement onCreateOptionsMenu(..) and onOptionsItemSelected(..) to handle the menu items for that fragment.

In the drawer layout:

  • Whenever you switch fragments, call invalidateOptionsMenu() on your activity.

If you're using support-v4 fragments, call supportInvalidateOptionsMenu(). There are some differences between Android versions in how they handle this stuff, so make sure you test on Ice Cream Sandwich, Jelly Bean and KitKat separately.

Post a Comment for "Implement A Different Action Bar Item In Each Fragment Of Navigation Drawer"