Josh Hansen
Josh Hansen

Reputation: 987

How to remove a specific android SubMenu from toolbar?

I have a Fragment that, when added to a view, should have certain accompanying menu items in the toolbar. So when the Fragment is created, I add a submenu to the activity's toolbar menu.

The problem is that if I leave and return to the fragment, then I get multiple instances of that submenu. So what I'd like to do is remove a specific submenu from the toolbar menu. All I've been able to find is a way to remove all items from the menu, but I don't want that either as there's one other item I'd like to retain.

Does anybody have a strategy for removing a specific submenu?

Upvotes: 0

Views: 58

Answers (1)

navylover
navylover

Reputation: 13629

The sub menu should not be created twice if you do correctly.

In your fragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true); //this line is important
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your sub menu entries here
    super.onCreateOptionsMenu(menu, inflater);
}

Upvotes: 3

Related Questions