This may be something really simple I'm not seeing, or something very weird I haven't been able to determine why happens.
I have an activity, and 3 fragments.
main_fragment
(a map fragment)left_menu_fragment
(listfragment used as main menu)right_menu_fragment
(listfragment used as context menu)
So just imagine something like facebook app, main content, and menus left and right.
When I change something in right_menu_fragment
, I want activity to let main_fragment
know that I changed something, so I created an interface.
Right menu do the callback, activity receives it, and call the proper function in main_fragment
, everything works perfectly so far.
Issue is, I am updating an object instance from right_menu_fragment
to another instance of same class in main_fragment
(actually, the variables have same name in both fragments)
When I do the callback, right menu calls this method in activity:
@Override
public void changedJourneyPreferences(JourneyPreferences journeyPreferences) {
if(mapsFragment != null) {
mapsFragment.setJourneyPreferences(journeyPreferences);
}
}
and the method in main_fragment
is implemented as follows:
public void setJourneyPreferences(JourneyPreferences journeyPreferences) {
//first log to verify BEFORE THE UPDATE
boolean foo = false;
//Some verifications new prefs vs old prefs
if(some conditions) {
foo = true;
}
//second log to verify I haven't made the update
this.journeyPreferences = journeyPreferences; //Do the update
//third log to verify I changed properly
//If verifications I made previosly are met
if(foo) {
//do some other things
}
}
As you see, I verify certain property of the variable I'm receiving to determine if a boolean
is true
or false
, and THEN and ONLY THEN, do the update of the variable.
I added the prints just to verify in runtime what was happening... well, those "other things" that are supposed to met sometimes never happen. I have encountered that once method gets called, it first updates the variable and then do the code in the method, or that is what I deduce.
Here is one sample log:
first log- old:0 new:0
second log- old:0 new:0
third log- old:0 new:0
first log- old:1 new:1
second log- old:1 new:1
third log- old:1 new:1
first log- old:2 new:2
second log- old:2 new:2
third log- old:2 new:2
When it should be something like:
first log- old:0 new:0
second log- old:0 new:0
third log- old:0 new:0
first log- old:0 new:1
second log- old:0 new:1
third log- old:1 new:1
first log- old:1 new:2
second log- old:1 new:2
third log- old:2 new:2
I tried also to comment the "do the update" line, and then it gets "solved", but of course never does the update.
I tried also to change variables names. EVERYWHERE. In right_menu_fragment
and main_fragment
, and in the setJourneyPreferences
function, receiving a variable name called differently. Didn't work.
So, long story in short: my method is working as if "do the update" line was the first one (as you clearly can see it's not)
Any help is appreciated.