Menu. addintentoptions Add a dynamic menu

Source: Internet
Author: User
Original menu. addintentoptions Add a dynamic menu to favorites

An activity in android can select another item and press the menu key to bring up a specific menu, that is, a dynamic menu. The Implementation of Dynamic menus is implemented by the addintentoptions function in the menu class. The specific declaration is as follows:
Int Android. View. Menu. addintentoptions (
Int groupid,
Int Itemid,
Int order,
Componentname caller,
Intent [] specifics,
Intent intent,
Int flags,
Menuitem [] outspecificitems)
This function is used to dynamically generate the option menu.

Function parameter analysis:

1. groupid is the number of the menu group;

2. Itemid (which can be set to 0)

3. Order menu order, not considered

4. Caller is the activity that initiates the activity.

5. Specifics uses Action + URI to add menu items to activate the corresponding activity.

6. Intent adds the menu item to activate the corresponding activity in the general form of categroy + Uri.

The difference between the intent parameter and specifics is that one uses categroy + URI to match the activity, and the other uses Action + URI to match the activity.

8. outspecificitems: The returned menuitem value, corresponding to the menu item that matches in specifics mode.

The following uses the notepad example in the android SDK to describe its usage.

Let's take a look at the noteslist in this example. the public Boolean onprepareoptionsmenu (menu) function in the Java file is called after the oncreateoptionsmenu function of the general option menu item is set. The main part of this function is the following code:

View plaincopy to clipboardprint?

1. Uri uri = contenturis. withappendedid (getintent (). getdata (), getselecteditemid ());
2.
3.
4. Intent [] specifics = new intent [1];
5. Specifics [0] = new intent (intent. action_edit, Uri );
6. menuitem [] items = new menuitem [1];
7.
8.
9. Intent intent = new intent (null, Uri );
10. Intent. addcategory (intent. category_alternative );
11. Menu. addintentoptions (menu. category_alternative, 0, 0, null, specifics, intent, 0,
12. items );

Uri uri = contenturis. withappendedid (getintent (). getdata (), getselecteditemid (); intent [] specifics = new intent [1]; specifics [0] = new intent (intent. action_edit, Uri); menuitem [] items = new menuitem [1]; intent = new intent (null, Uri); intent. addcategory (intent. category_alternative); menu. addintentoptions (menu. category_alternative, 0, 0, null, specifics, intent, 0, items );

Contenturis. withappendedid (getintent (). getdata (), getselecteditemid () will get the information of the selected item. This information will apply to the intent

Data section.

Specifics [0] = new intent (intent. action_edit, Uri) Here is the meaning: Go to androidmenifest. XML to find the activity, if there is an activity If the action and data of the item match with intent. action_edit and the corresponding Uri, a menu item is added for the activity. The display name of the menu item is

Android: Label item.

Intent intent = new intent (null, Uri );

Intent. addcategory (intent. category_alternative );

To search for the activity in androidmenifest. xml. Item category and Data

Intent. category_alternative matches with the corresponding URI to add menu items for these activities respectively. The display name of menu items is from the Android: Label item of the corresponding activity.

.

The following is a test to create an activity myadd in androidmenifest. xml.

View plaincopy to clipboardprint?

1.
3.
4.
5.
6.
7.
8.

After writing the layout and implementation of the activity, select one of the noteslist items, click menu to view the additional items in the menu, and click it to switch to the activity.

This is a menu item that matches intent in the function, and can also be matched using specifics. The following example:

Delete The activity does not match the intent,

Then, change the code in the onprepareoptionsmenu function to the following:

View plaincopy to clipboardprint?

1. Intent [] specifics = new intent [2];
2. Specifics [0] = new intent (intent. action_view, Uri );
3. Specifics [1] = new intent ("com. Android. notepad. Action. myadd", Uri );
4. menuitem [] items = new menuitem [2];

Intent [] specifics = new intent [2]; specifics [0] = new intent (intent. action_view, Uri); specifics [1] = new intent ("com. android. notepad. action. myadd ", Uri); menuitem [] items = new menuitem [2];

After selecting a menu item, you will find that the dynamic menu is back, but this time it is matched by specific.

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.