Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm new on here so apologies if I don't format everything in top-notch. I too have had similar questions regarding menu contributions and <code>Fragment</code>s. Here is what I did in my instance...</p> <p>I have a <code>FragmentActivity</code> hosting a <code>ListFragment</code> and an additional <code>Fragment</code> containing details of the list item selected- pretty typical tablet config. This additional detail <code>Fragment</code> is not added until a list item is selected... When I select a list item I want to prevent the <code>ListFragment</code>s menu items from contributing to the hosting <code>FragmentActivity</code> Action Bar. (From a UI perspective the users main focus is the newly added Fragment, to return to the previous state they deselect the item they selected.) Since the <code>ListFragment</code> should not know about the other Fragment to ensure re-usability I found that leveraging the <code>onPrepareOptionsMenu(..)</code> method at the <code>FragmentActivity</code> level was the correct way forward for me.</p> <p>Add this snippet of code to the hosting <code>FragmentActivity</code> to quickly get the behaviour you want;</p> <pre><code>@Override public boolean onPrepareOptionsMenu(Menu menu) { if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { // 'Find' the menu items that should not be displayed - each Fragment's menu has been contributed at this point. menu.findItem(id.sync).setVisible(false); } return super.onPrepareOptionsMenu(menu); } </code></pre> <p>This method means that your Fragments remain reusable (they are still contributing as initially designed) but your host <code>FragmentActivity</code> can manipulate the Activity UI which is one of it's authorised roles.</p> <p>I hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload