How can I change the action bar actions dynamically?

I have Activity with ActionBar and tab navigation. I use split mode, so the tabs are at the top and the actions are in the bottom pane. How can I dynamically change the bottom actions? I need this because each tab has different actions.

+62
android android-actionbar
Nov 26 '11 at 16:49
source share
4 answers

Since actions are populated with an action options menu, you can use Activity#invalidateOptionsMenu() . This will unload the current menu and call the onCreateOptionsMenu / onPrepareOptionsMenu methods onCreateOptionsMenu to rebuild it.

If you use the action bar tabs to better change the fragment configuration. Each fragment controls its part of the menu. These fragments should call setHasOptionsMenu(true) . When fragments with options menu items are added or removed, the system automatically cancels the options menu and calls all onCreateOptionsMenu / onPrepareOptionsMenu in addition to the action. Thus, each fragment can manage its own elements, and you do not need to worry about manually switching the menu.

+129
Nov 26 '11 at 18:40
source share

Activity.invalidateOptionsMenu () requires API level 11. There is a simpler solution that is backward compatible:

First add a MenuItem to the menu, but set the visibility to false . If desired, set the visibility to true using MenuItem.setVisible ()

+15
Mar 16 '13 at 20:57
source share

ActionMode.invalidate () did the trick. This caused a call to onPrepareActionMode() .

Activity#invalidateOptionsMenu() did not to call onPrepareActionMode() when using list items with multi-select.

+1
Dec 17 '12 at 12:01
source share

Activity.invalidateOptionsMenu() requires API level 11. Use its support version supportInvalidateOptionsMenu () .

 AppCompatActivity activity = (AppCompatActivity) getActivity(); activity.supportInvalidateOptionsMenu(); 
+1
Nov 27 '15 at 19:47
source share



All Articles