Pages

Thursday, March 17, 2016

Android multiple fragments with lists, context menu uses same onContextItemSelected method

I have several fragments in one activity, each with a listview. Each listview has context menu on long click. So far all works ok. But when i select an option from the menu, only one fragments onContextItemSelected method is called, and it uses it's activity's list.

How it should work - the onContextItemSelected should return false if it is not the correct one to be used. I found the answer here:

http://stackoverflow.com/questions/25096534/context-menu-in-fragment-uses-listview-from-a-different-fragment-registerforcon

This way all the unwanted methods will return false and the one you need will be invoked.

@Override
public boolean onContextItemSelected(MenuItem item) {
    if (getUserVisibleHint()) {
            //your code
        }
    }
    return false;
}

No comments:

Post a Comment