How to hide the fragment bar button when the navigation drawer is open


I use Navigation Drawer and for one of the fragments I have different menu. For that fragment when navigation drawer is open, how to hide the action bar's button? For example on the picture ( ) it is button REFRESH.

@Override public void onCreate(Bundle savedInstanceState) {
**setHasOptionsMenu(true);** }

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
**inflater.inflate(, menu);**

The best way to update your actionBar buttons is to call the invalidateOptionsMenu() when your NavigationDrawer is opened/closed.

According to the Android training documentation page, when you create your ActionBarDrawerToggle, you should override the onDrawerClosed and the onDrawerOpened methods:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { 

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

Also, in your onPrepareOptionsMenu method, you can detect if your drawer is open or not, and load the menu corresponding to your drawer state:

/* Called whenever we call invalidateOptionsMenu() */
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        return super.onPrepareOptionsMenu(menu);

For more information, you can see the Android developper documentation here : (cf section: Listen for Open and Close Events)