Kris B
Kris B

Reputation: 3578

Handling fragment menus options

I have two fragments, one a ListView and one a "details" view. Each fragment has it's own menu options. When in landscape mode, where both fragments are displayed, each menu is merged into one (which, I'm assuming is by design), but if I rotate the device to portrait, the menus remain merged, and show options that shouldn't be there. Not sure if I need to include any code in my question.

Edit:

Found this question, but adding menu.clear() only shows the menu options for the details view, when in landscape and changing back to portrait, the menu doesn't change.

I guess the bigger question is how to do you handle fragments, each with the own set of menu options.

Update:

To be clear, the menus work fine if the device is left in portrait or landscape mode. The issue occurs when rotating from landscape to portrait or vice versa. The menus do not seem to reset even though onCreateOptionsMenu is called.

Upvotes: 3

Views: 9887

Answers (3)

BrantApps
BrantApps

Reputation: 6472

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 Fragments. Here is what I did in my instance...

I have a FragmentActivity hosting a ListFragment and an additional Fragment containing details of the list item selected- pretty typical tablet config. This additional detail Fragment is not added until a list item is selected... When I select a list item I want to prevent the ListFragments menu items from contributing to the hosting FragmentActivity 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 ListFragment should not know about the other Fragment to ensure re-usability I found that leveraging the onPrepareOptionsMenu(..) method at the FragmentActivity level was the correct way forward for me.

Add this snippet of code to the hosting FragmentActivity to quickly get the behaviour you want;

@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);
}

This method means that your Fragments remain reusable (they are still contributing as initially designed) but your host FragmentActivity can manipulate the Activity UI which is one of it's authorised roles.

I hope that helps.

Upvotes: 2

Joey
Joey

Reputation: 697

Since you want to refresh the inflated menus to the action bar every time the orientation change try to declare that your activity in the manifest holding the fragments to listen in configuration change

Then override the method onConfigurationChanged, and use the invalidateOptionMenu API see this link:

Activity invalidate options menu:

http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu%28%29

If this helps you, please mark this post as an answer.

Thanks.

Upvotes: 0

Kris B
Kris B

Reputation: 3578

Figured out this issue, if you are using setRetainInstance(true) then the menus will not refresh. Unfortunately, removing this break other things such as my AsyncTask is killed when changing orientation.

Upvotes: 1

Related Questions