Adjusting Slice Menu Options

I have two fragments: one ListView and one "detail" view. Each fragment has its own menu options. When in landscape mode, where both slices are displayed, each menu merges into one (which I assume by design), but if I rotate the device to portrait, the menus stay 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 viewing details when in landscape and changing to portrait, the menu does not change.

I guess the more important question is how you handle snippets, each with its own set of menu options.

Update:

To be clear, the menu works great if the device stays in portrait or landscape mode. The problem occurs when turning from landscape to portrait or vice versa. The menus do not appear to be reset, although it is called onCreateOptionsMenu

.

+3


source to share


3 answers


Please find out this problem if you are using setRetainInstance(true)

then the menu will not update. Unfortunately, changing orientation removes other things like my AsyncTask.



+1


source


I'm a newbie here, so I apologize if I don't format everything in a first class way. I also had similar questions regarding menu input and Fragment

s. Here's what I did in my case ...

I have FragmentActivity

hosting a ListFragment

and an optional one Fragment

containing the details of a selected list of items - a fairly typical tablet configuration. This extra detail is Fragment

not added until the list item is selected ... When I select the list item, I want the menu items ListFragment

not to contribute to the action bar FragmentActivity

. (From a UI perspective, users focus on the newly added snippet, in order to revert to the previous state, they deselected the selected item.) Since ListFragment

they shouldn't be aware of another snippet to ensure reuse, I found that using a method onPrepareOptionsMenu(..)

at the level FragmentActivity

was the correct way for me.

Add this code snippet to your hosting FragmentActivity

to quickly get the behavior 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 menu has been contributed at this point. 
   menu.findItem(id.sync).setVisible(false);
 }
return super.onPrepareOptionsMenu(menu);
}

      

This method means your fragments remain reusable (they still contribute as originally designed), but your host FragmentActivity

can manipulate the activity's UI, which is one of its authorized roles.

I hope this helps.

+2


source


Since you want to update the inflated menus in the action bar every time the orientation change tries to declare your activity in the manifest containing the snippets to listen on when the configuration changes

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

Invalid action menu:

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

If this helps you, please mark this post as a response.

Thank.

0


source







All Articles