How can I change Action Bar actions dynamically?
Asked Answered
B

4

62

I have an Activity with ActionBar and tab navigation. I am using the split mode, so the tabs are at the top and actions are in the bottom bar. How can I dynamically change the bottom actions? I need this because every tab has different actions.

Blakley answered 26/11, 2011 at 16:49 Comment(0)
D
129

Since the actions are populated by the activity's options menu you can use Activity#invalidateOptionsMenu(). This will dump the current menu and call your activity's onCreateOptionsMenu/onPrepareOptionsMenu methods again to rebuild it.

If you're using action bar tabs to change your fragment configuration there's a better way. Have each fragment manage its own portion of the menu. These fragments should call setHasOptionsMenu(true). When fragments that have options menu items are added or removed the system will automatically invalidate the options menu and call to each fragment's onCreateOptionsMenu/onPrepareOptionsMenu methods in addition to the activity's. This way each fragment can manage its own items and you don't need to worry about performing menu switching by hand.

Disregard answered 26/11, 2011 at 18:40 Comment(6)
Just a note: setHasOptionsMenu() should be called in onCreate according to the documentation.Lozoya
That's not actually a requirement. It may be called from there and it is a good place to do so in many circumstances, but if the fragment has already been added and is not hidden the activity's options menu will be invalidated and the new state will be reflected.Disregard
I agree with you, however the documents don't make it sound like that. "In order for this method to receive calls, however, you must call setHasOptionsMenu() during onCreate() ...". I think they mean you must call setHasOptionsMenu() but they unintentionally made it sound like during onCreate() was also a requirement.Lozoya
Calling setHasOptionsMenu doesn't work if you've already called it once with true and want to update them. At least this fails with the Android Support Library v4. In that case, use getActivity().supportInvalidateOptionsMenu() which is what setHasOptionsMenu does internally, anyway.Reflector
That's correct. Invalidating the options menu is the way to go if something has changed.Disregard
calling invalidateOptionsMenu()does not invoke onPrepareOptionsMenu when called from a menu action, see @AlikElzin-kilaka comment : call ActionMode.invalidate() insteadRefuge
O
16

Activity.invalidateOptionsMenu() requires API Level 11. There is a simpler solution which is backwards compatible:

Add the MenuItem to the Menu initially but set its visibility to false. Set visibility to true when desired, using MenuItem.setVisible()

Occultism answered 16/3, 2013 at 20:57 Comment(2)
Thanks. Just in case helps others... Put MenuItem refreshItem = menu.findItem(R.id.action_refresh); refreshItem.setVisible(false); in your onCreateOptionsMenuSimmon
@Simmon or "android:visible"="false" in xmlCession
L
1

ActionMode.invalidate() did the trick. It caused the onPrepareActionMode() to be invoked again.

Activity#invalidateOptionsMenu() did not cause the onPrepareActionMode() to be invoked when using list items with multi-select.

Lang answered 17/12, 2012 at 12:1 Comment(4)
how do I get actionmode? I only have ActionBarHectoliter
The ActionMode is the first parameter in onPrepareActionMode(...): goo.gl/tMyBxLang
I have never heard of onPrepareActionDiadromous
@Diadromous - I answered the the action mode way, meaning CAB - Contextual Action Bar. It's when acting on selected list items, thus changing the list of actions.Lang
E
1

Activity.invalidateOptionsMenu() requires API Level 11. Use the Support library version of it supportInvalidateOptionsMenu().

AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.supportInvalidateOptionsMenu();
Erbes answered 27/11, 2015 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.