From aee303fb62cdff9fc0981016cdf3d079d8be4d8d Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Sun, 7 Feb 2016 14:07:54 +0100 Subject: [PATCH] Call onPrepareOptionsMenu on consumer before showing the menu Otherwise the 'sort' option will stay enabled, causing crashes in the files adapter --- .../android/vanilla/BottomBarControls.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/BottomBarControls.java b/src/ch/blinkenlights/android/vanilla/BottomBarControls.java index 40ecbb66..7277d905 100644 --- a/src/ch/blinkenlights/android/vanilla/BottomBarControls.java +++ b/src/ch/blinkenlights/android/vanilla/BottomBarControls.java @@ -70,6 +70,10 @@ public class BottomBarControls extends LinearLayout * Standard android search view */ private SearchView mSearchView; + /** + * The assembled PopupMenu + */ + private PopupMenu mPopupMenu; /** * ControlsContent click consumer, may be null */ @@ -102,7 +106,7 @@ public class BottomBarControls extends LinearLayout public void onClick(View view) { Object tag = view.getTag(); if (tag instanceof PopupMenu) { - ((PopupMenu)tag).show(); + openMenu(); } else if (tag instanceof MenuItem) { mParentMenuConsumer.onOptionsItemSelected((MenuItem)tag); } else if (view == mControlsContent && mParentClickConsumer != null) { @@ -148,15 +152,15 @@ public class BottomBarControls extends LinearLayout mParentMenuConsumer = owner; ImageButton menuButton = getImageButton(getResources().getDrawable(R.drawable.ic_menu_moreoverflow)); - PopupMenu popupMenu = (menuMargin() ? new PopupMenu(mContext, menuButton, Gravity.RIGHT) : new PopupMenu(mContext, menuButton)); - popupMenu.setOnMenuItemClickListener(this); + mPopupMenu = (menuMargin() ? new PopupMenu(mContext, menuButton, Gravity.RIGHT) : new PopupMenu(mContext, menuButton)); + mPopupMenu.setOnMenuItemClickListener(this); // Let parent populate the menu - mParentMenuConsumer.onCreateOptionsMenu(popupMenu.getMenu()); + mParentMenuConsumer.onCreateOptionsMenu(mPopupMenu.getMenu()); // The menu is now ready, we an now add all invisible // items to the toolbar - Menu menu = popupMenu.getMenu(); + Menu menu = mPopupMenu.getMenu(); for (int i=0; i < menu.size(); i++) { MenuItem menuItem = menu.getItem(i); if (menuItem.isVisible() == false) { @@ -168,7 +172,7 @@ public class BottomBarControls extends LinearLayout } // Add menu button as last item - menuButton.setTag(popupMenu); + menuButton.setTag(mPopupMenu); menuButton.setOnClickListener(this); int specialSnowflake = menuMargin() ? dpToPx(36) : LinearLayout.LayoutParams.WRAP_CONTENT; menuButton.setLayoutParams(new LinearLayout.LayoutParams(specialSnowflake, LinearLayout.LayoutParams.WRAP_CONTENT)); @@ -189,8 +193,10 @@ public class BottomBarControls extends LinearLayout * Opens the OptionsMenu of this view */ public void openMenu() { - // simulates a click on the 2nd rightmost child which should be the options menu - mControlsContent.getChildAt(mControlsContent.getChildCount()-2).performClick(); + if (mPopupMenu == null || mParentMenuConsumer == null) + return; + mParentMenuConsumer.onPrepareOptionsMenu(mPopupMenu.getMenu()); + mPopupMenu.show(); } /**