0

I'm displaying an Activity containing a ViewPager. In one of the Pager Fragment I have a ListView with a ContextMenu.

Long pressing on a List item displays the ContextMenu (onCreateContextMenu() is called), but selecting an entry in the ContextMenu doesn't call onContextItemSelected()

Using the same fragment outside the ViewPager works fine. Is there something special to do in case the Fragment is embedded in a ViewPager ?

user1026605
  • 1,633
  • 4
  • 22
  • 58
  • 1
    Post your code please. Layouts would be helpful too. – Simas Mar 11 '15 at 19:34
  • the layouts are simple. The one is a viewpager. In the ViewPager I display a fragment which layout is just a listview. It works fine, but when embeded in a ViewPager, the ContextMenu, is created and displayed, but when I press on an entry onContextItemSelected() isn't called. I'm just asking if anyone has an idea how that can happen – user1026605 Mar 11 '15 at 20:40
  • 2
    Oh so I guess you don't want the problem resolved just want someone to ponder with you. Okay, fine by me :) – Simas Mar 11 '15 at 20:46
  • Why would someone need any code to answer a question ? Not every question requires some code – user1026605 Mar 11 '15 at 21:41
  • When you post code, we can actually run the code ourselves, get it under a debugger and identify the exact problem. – kris larson Mar 11 '15 at 23:11
  • Did you at least read the question before answering something like that ? I provided all the useful information and someone figure it out quickly. Once again not every question requires some code if everything is well explained... – user1026605 Mar 16 '15 at 10:50

1 Answers1

3

I hacked some code together and had a similar problem. I registered the ListView in Fragment 2 for context menu and unregistered Fragment 1. Yet somehow onContextItemSelected() was being called on Fragment 1.

Turns out that when the FragmentManager dispatches a ContextItemSelected event, it calls onContextItemSelected() on every single Fragment it knows about until one of them returns true. So in your onContextItemSelected(), you have to check if the fragment is the current page in the ViewPager; if it isn't, return false. This can be one source of problems.

See this SO question: Wrong fragment in ViewPager receives onContextItemSelected call

Hope that's helpful

Community
  • 1
  • 1
kris larson
  • 30,387
  • 5
  • 62
  • 74
  • None of some methods are deprecated. I don't want to change the whole app, I'm just looking for a way to make this work... or at least understand why it doesn't – user1026605 Mar 12 '15 at 07:05
  • Since I took the time to write the code to find your problem, don't you think I deserve that bounty? – kris larson Mar 19 '15 at 01:50