0

I have 3 Fragments which, in a normal (small) layout are all in separate Activities. They should provide an optionsmenu in the small layout.

In the large layout, I have the 3 fragments in one Activity, causing the menu to fill with the buttons inflated by all three fragments. How can I prevent this, and only let the Activity inflate the optionsmenu, while still holding the functionality on smaller devices?

-Edit-

So each Fragment uses the following code:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    /* Some code */
    setHasOptionsMenu(true);
    /* Some code */
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
    inflater.inflate(R.menu.mymenu, menu);
}

When all three Fragments are shown, all Fragments execute onCreateOptionsMenu() and all items appear three times. What I want is for the parent Activity to take the responsibility of creating the options menu.

nhaarman
  • 98,571
  • 55
  • 246
  • 278

2 Answers2

2

In each Fragment I would build the entire menu. What I've done is, to add in the Fragments only their specific menu items, and in the parent Activity I would build the general items.

nhaarman
  • 98,571
  • 55
  • 246
  • 278
0

You could use the isVisible() method on each Fragment to dynamically check which is being displayed and then add appropriately within the Activity.

telkins
  • 10,440
  • 8
  • 52
  • 79