1

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.

4

2 回答 2

0

好的,我发现了错误。

将局部变量更新为我作为参数接收的变量时,我将自己的变量分配给我接收的变量(结果与另一个片段完全相同)。

所以我不知道这到底是如何工作的,但是我的局部变量变成了指针,相同的 Id,相同的数据,相同的一切。两个不同片段中的两个变量指向完全相同的数据。当我setJourneyPreferences从活动中调用时,变量已经更新,这就是我在更新之前无法验证任何内容的原因。

我解决它的方式,创建一个从参数传递值的新实例。略有变化,但绝对不同:

替换此行:

this.journeyPreferences = journeyPreferences; //Do the update

有了这个:

//Do the update
this.journeyPreferences = new JourneyPreferences(
journeyPreferences.getMyLocationAs(),
journeyPreferences.getTimeOfJourneyOpts(),
journeyPreferences.getTimeOfJourney());

复制数据,仅此而已。

于 2013-07-29T14:58:32.947 回答
0

JourneyPreferences.getMyLocationAs() 做什么?您的问题可能与该类中的静态变量有关吗?

于 2013-07-25T07:26:58.693 回答