Tuesday, 26 March 2019

How to use MenuItem.setIntent() in a ContextMenu

I have a context menu with a menu item that is supposed to start an intent:

@Override
public void onCreateContextMenu(ContextMenu menu, View view,
        ContextMenuInfo menuInfo) {
    MenuItem item = menu.add(menuItemText);
    item.setIntent(intent);

This used to work, but in newer Android versions, it throws an exception:

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Adding this flag would be the easy way out, but I don't want to create a new task since this is supposed to be part of the application's activity stack. The problem is that the system calls startActivity which the context of the item, which in this case is a DecorContext, not an Activity. Since this is all handled by the framework, I have no control over this.

Is there a good solution to this other than setting the flag or handling the operation in onContextItemSelected?



from How to use MenuItem.setIntent() in a ContextMenu

No comments:

Post a Comment