How Android dynamically changes the item icon on the Actionbar

Source: Internet
Author: User

1.Activity menu mechanism (similar to dialog)
Activity has a mechanism for managing menus, as follows:
1.public boolean Oncreateoptionsmenu (Menu menu)
This method is used to initialize the menu, where the menus parameter is the menu instance that is about to be displayed.
Returns true to show that the menu,false is not displayed;
(only called when the menu is initialized for the first time)

2.public boolean Onprepareoptionsmenu (Menu menu)
After the Oncreateoptionsmenu is executed, the menu is called before it is displayed, and if the menu is already created, it is called before the menu is displayed.
Similarly, returns true to show that the menu,false is not displayed;
(This method can dynamically change the state of the menu, such as loading different menus, etc., called after the Oncreateoptionsmenu call)

3.public void onoptionsmenuclosed (Menu menu)
Called each time the menu is closed.
(There are three scenarios when the menu button is clicked again, the Back button is clicked, or the user selects a menu item)

4.public boolean onoptionsitemselected (MenuItem Item)
The menu item is invoked when clicked, that is, the method of listening for the food item.

With these methods, it can be learned that for activity, only one menu object can be displayed and monitored at the same time.

But this oncreateoptionsmenu is only called once during the entire cycle of activity and does not change, so there is a Onprepareoptionsmenu method in the newer version of Android.

The Oncreateoptionsmenu method is unable to dynamically load the menu, because he only calls once, you can change Onprepareoptionsmenu this method to get, he every click on the menu will be called.

From the above analysis, we can change the item in Onprepareoptionsmenu, but how do I call Onprepareoptionsmenu when I want to change the menu item?

The solution is to add the following where you want to update the menu items:

Mactivity.getwindow (). Invalidatepanelmenu (Window.feature_options_panel);

This is the native Actionbar solution, if you are using Actionbarsherlock, then call

invalidateOptionsMenu();

/** * When the article refreshes, if the show ic_menu_fav_pressed icon*/@Override Publicboolean Onprepareoptionsmenu (Menu menu) {System. out. println ("executed the Onprepareoptionsmenu"); if(Collectiondb.ishasartivle (Article.getid ())) {Menu.finditem (r.id.action_fav). SetIcon (            r.drawable.ic_menu_fav_pressed); Menu.finditem (r.id.action_fav). Setcheckable (false); } Else{Menu.finditem (r.id.action_fav). SetIcon (R.DRAWABLE.IC_MENU_FAV); Menu.finditem (r.id.action_fav). Setcheckable (true); }        //getsupportmenuinflater (). Inflate (R.menu.book_detail, menu);        returnsuper.onprepareoptionsmenu (menu); } @Override Publicboolean Oncreateoptionsmenu (Menu menu) {System. out. println ("executed the Oncreateoptionsmenu");        Getsupportmenuinflater (). Inflate (R.menu.book_detail, menu); return true; }

How Android dynamically changes the item icon on the Actionbar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.