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

后端 未结 10 1369
予麋鹿
予麋鹿 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条回答
  • 2020-12-02 12:42

    Based on "Klaasvaak" answer above. I am using its subMenus. This works for me :

    // Declare and save the menu as global, so it can be called anywhere.
    Menu absTopSubMenus;
    
    public boolean onCreateOptionsMenu(Menu menu) {
    
    absTopSubMenus = menu;  // Used for re-drawing this menu anywhere in the codes.
    
    // The remainder of your code below
    }
    

    Then, to re-draw this, just call :

    // Redraw the top sub-menu
    absTopSubMenus.clear();
    onCreateOptionsMenu(absTopSubMenus);
    
    0 讨论(0)
  • 2020-12-02 12:46

    save a reference to the menu and call:

    this.menu.clear();
    this.onCreateOptionsMenu(this.menu);
    
    0 讨论(0)
  • 2020-12-02 12:47

    You can now use supportInvalidateOptionsMenu() method from the ActionbarActivity with the support library. So you don't have to check for the version of the sdk. Goes until API 7 and above.

    http://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html

    do to so, you have to import the v7 of the support library using this

    http://developer.android.com/tools/support-library/setup.html

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

    Use

    ActivityCompat.invalidateOptionsMenu(Activity activity)
    

    from the compatibility library.

    See: https://stackoverflow.com/a/14748687/435855

    0 讨论(0)
提交回复
热议问题