0

I have a single instance of a custom class, 'encounter' declared in a Fragment, as seen below:

public class EncounterStrategyFragment extends Fragment
                                   implements DialogConfirm.NoticeDialogListener{

    private MalifauxEncounter encounter;

This object contains a single List. It is supplied to a custom List adapter which is working as expected. The ListView the adapter is attached to has been registered for a Context Menu, via registerForContextMenu(listview);.

My problem is that 'encounter's address/id is changing inside the OnContextItemSelected callback method. Here is 'encounter's state everywhere else in the Fragment: (id=830047711760, array size = 10)

But inside OnContextItemSelected it becomes this: (id=830055767320, internal array size = 0)

I'd post pictures, but my reputation isn't high enough...

Outside of OnContextItemSelected the address is the same as it initially was, so the 'encounter Object is not being re-created; it's only different for this single method. So any modifications I try to perform inside OnContextItemSelected cause an error.

My implementation was working properly before I made this fragment part of an ActionBar tab structure.

What the heck is going on?

EDIT: Resolution: The root cause of my issue was that the Fragment was being created twice by the Action Bar tab manager code. The OnContextItemSelected was using the 'encounter' variable from the 2nd instance of the Fragment, whereas the other source was using the copy from the first instance. Resolved using this post: Fragment onCreateView and onActivityCreated called twice

Community
  • 1
  • 1
  • 1
    Post the minimum amount of code necessary to replicate the issue. While you're carving away at your code, you may well find the source of the problem on your own. – MarsAtomic Jun 17 '14 at 01:12
  • while you are trying to make a minimal example to show us what is going on, please post your `onContextItemSelected()` method. – Code-Apprentice Jun 17 '14 at 01:55

2 Answers2

0

The root cause of my issue was that the Fragment was being created twice by the Action Bar tab manager code. The OnContextItemSelected method was using the 'encounter' variable from the 2nd instance of the Fragment, whereas the other source was using the copy from the first instance. Resolved using this post: Fragment onCreateView and onActivityCreated called twice

Community
  • 1
  • 1
0

Probably somebody stuck with wrong data returned in onContextItemSelected. In my case I used ViewPager based on one fragment and context menu inside.

If this is your case, see Wrong fragment in ViewPager receives onContextItemSelected call.

For instance,

override fun onContextItemSelected(item: MenuItem?): Boolean {
    return if (userVisibleHint) {
        // Handle menu events and return true
        true
    } else
        false // Pass the event to the next fragment
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224