How can I refresh the ActionBar when onPrepareOptionsMenu switched menu entries?

后端 未结 10 1368
予麋鹿
予麋鹿 2020-12-02 12:05

Within my apps I often enable/disable menu entries and do make them visible from onPrepareOptionsMenu.

Today I started to add the android:showAsAction menu attribute

相关标签:
10条回答
  • My method of choice is to create a helper class. For example:

    class VersionHelper
    {
        static void refreshActionBarMenu(Activity activity)
        {
            activity.invalidateOptionsMenu();
        }
    }
    

    Now in your code above, replace invalidateOptionsMenu(); with:

    if (Build.VERSION.SDK_INT >= 11)
    {
        VersionHelper.refreshActionBarMenu(this);
    }
    

    Credit for this method goes to CommonsWare (search for HoneycombHelper, and check out his books - highly recommended)

    0 讨论(0)
  • 2020-12-02 12:25

    Thanks to the accepted answer. I am using ActionBarActivity. in this class you can use

    supportInvalidateOptionsMenu();
    
    0 讨论(0)
  • 2020-12-02 12:29

    getActivity().invalidateOptionsMenu();

    gets the job done.

    0 讨论(0)
  • 2020-12-02 12:29

    Also make sure you are not calling

    myAppCompatActivity.setToolbarTitle("some dynamic title");
    

    shortly after you have refreshed your menu.

    I had the issue that the drawables would not show up even if there was room for them to be displayed. Once I did an orientation change the drawables then appeared.. ?

    In Summary:

    MainActivity:

    public class MainActivity extends AppCompatActivity {
    
        private boolean showMenu = true;
    
        public void setShowMenu(boolean show) {
            showMenu = show;
            supportInvalidateOptionsMenu();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.my_menu, menu);
            menu.findItem(R.id.menu_share).setVisible(showMenu);
            // menu.findItem(...
            return true;
        }
    }
    

    FragmentNoMenu:

    public abstract class FragmentNoMenu extends Fragment {
    
        protected MainActivity mainActivity;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
            mainActivity = (MainActivity) getActivity();
            if (mainActivity != null) {
                mainActivity.setShowMenu(false);
            }
        }
    
    }
    

    FragmentWithMenu:

    public abstract class FragmentWithMenu extends Fragment {
    
        protected MainActivity mainActivity;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
            mainActivity = (MainActivity) getActivity();
            if (mainActivity != null) {
                mainActivity.setShowMenu(true);
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 12:30

    Kudos to @Klaasvaak for showing us the way here. I use the following which works on both pre- and post- API Level 11:

    private void invalidOptionsMenuHelper() {
        if (Build.VERSION.SDK_INT >= 11) {
            invalidateOptionsMenu();
    
        } else if (mOptionsMenu != null) {
            mOptionsMenu.clear();
            onCreateOptionsMenu(mOptionsMenu);
        }
    }
    

    Of course, you must save a reference to the menu (mOptionsMenu in this case) which I accomplish via:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mOptionsMenu = menu;
    
        //... create and manage the menu normally
    }
    
    0 讨论(0)
  • 2020-12-02 12:32

    I am not sure you have seen it already, but if you use the actionbar extensively, and plan on supporting lower API's (>8), take a look at the actionBarSherlock library. It will make it so you do not have to section your actionBar code for lower and Higher API's, and then you can just run:

    runOnUiThread(new Runnable(){
      @Override
      public void run(){
        supportInvalidateOptionsMenu();
      }
    });
    
    0 讨论(0)
提交回复
热议问题