Menus in Context
By and large, context menus use the same guts as option menus. The two main differences are how you populate the menu and how you are informed of menu choices.
First you need to indicate which widget(s) on your activity have context menus. To do this, call from your activity, supplying the that is the widget in need of a context menu.
Next you need to implement , which, among other things, is passed the you supplied in . You can use that to determine which menu to build, assuming your activity has more than one.
The method gets the itself, the the context menu is associated with, and a , which tells you which item in the list the user did the tap‑and‑hold over, in case you want to customize the context menu based on that information. For example, you could toggle a checkable menu choice based on the current state of the item.
It is also important to note that gets called each time the context menu is requested. Unlike the options menu (which is built only once per activity), context menus are discarded once they are used or dismissed. Hence, you do not want to hold onto the supplied object; just rely on getting the chance to rebuild the menu to suit your activity’s needs on demand based on user actions.
To find out when a context‑menu choice was selected, implement on the activity. Note that you get only the instance that was chosen in this callback. As a result, if your activity has two or more context menus, you may want to ensure they have unique menu‑item identifiers for all their choices so you can tell them apart in this callback. Also, you can call on the to get the you received in . Otherwise, this callback behaves the same as , described in the previous section.
Дата добавления: 2015-05-16; просмотров: 872;