Question

In older apps I would typically use onCreateContextMenu() with the following signature for context actions:

public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenuInfo menuInfo)

The menuInfo would provide me the information for which item in my list the context menu was being built. I could then get the right Uri to find the actions that are appropriate for it and create them as menu items (using automatic intents or whatever).

Now, with the newer APIs, the replacement for this is the contextual action bar using ActionMode:

public boolean onCreateActionMode(ActionMode mode, Menu menu) 
public boolean onPrepareActionMode(ActionMode mode, Menu menu)

But here, how can I distinguish the item and/or get this position info?

For batch mode, such a parameter not being present, makes sense, because you should only offer actions that are applicable to anything. But for the single mode, wouldn't this be entirely equivalent to the old API, except the buttons are now in the action bar?

Reference: http://developer.android.com/guide/topics/ui/menus.html#context-menu

Was it helpful?

Solution

Even for a "batch" selection, you still need to find out what item(s) are selected to perform the desired action on.

As far as I know, the part "what item(s) are selected" is not addressed by these callbacks and is left to the developer; there is no generic way for marking items and a common pattern is using check boxes. Note that the state of "selected" for the list items shouldn't be a transient state in the sense that if you click on an item to mark is as "selected", it should remain selected visually, so that user can understand which item is being worked on. So you can either trick a normal ListView to keep its "selected" state or use check boxes or similar mechanisms. In any of these approaches, whether it is one or many selected items, you have your list and you can interrogate your list to obtain which ones are selected and perform the desired action on them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top