Question

I made an application using navigation drawer, open the drawer when I display the "Settings" menu


when showing the fragment in my container displays two menu "settings" and "search information". http://saifulhq.wordpress.com/?attachment_id=242


but when i click the back button on the header and then appeared drawer, 3 items such as pictures "Setting", "Setting", "Search Information"
http://saifulhq.wordpress.com/?attachment_id=243


My question is how to remove a menu item from a fragment then add menu items from the drawer?
The following is the code on the drawer to display menu items

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // If the drawer is open, show the global app actions in the action bar.
    // See also
    // showGlobalContextActionBar, which controls the top-left area of the
    // action bar.
    if (mDrawerLayout != null && isDrawerOpen()) {
        inflater.inflate(R.menu.global, menu);
        showGlobalContextActionBar();
    }
    super.onCreateOptionsMenu(menu, inflater);
}

this is showGlobalContextActionBar()

/**
 * Per the navigation drawer design guidelines, updates the action bar to
 * show the global app 'context', rather than just what's in the current
 * screen.
 */
private void showGlobalContextActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setTitle(R.string.app_name);
}
Was it helpful?

Solution

In order for what you are doing to work you must move your menu creation to onPrepareOptionsMenu() as that is the method that is called every time your menu is created because OnCreateOptionsMenu() is only called once.

In order to remove a menu item from the menu just save the menu and call removeItem()

Here is a reference http://developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top