I have been trying to fix a bug in my application for already 3 days, with no success. The problem is that an instance variable gets magically assigned.
The application is a bluetooth multiplayer game in which the players take turn. After the game is over a dialog is shown to ask the players if they would like to continue. So when the player 1 continues, a message is sent to player 2 to notify that player 1 wants continue. The variable "opponentContinued" gets true. And when the player 2 clicks continue too, if opponentContinued equals true the game restarts.
The problem :
When the dialog is shown to both of the players and player 1 rotates the screen, player 2 clicks continue, player 1 receives notification and "opponentContinued" gets assigned as true. AFTER this when player 1 clicks continue and (opponentContinued must be true) checks if "opponentContinued" is true, finds that "opponentContinued" is false.
Everything works fine if there is no rotation.
Then to make it a bit clear I made a dummy variable to monitor when and how "opponentContinued" gets assigned and every time it got assigned I made dummyBoolean equal to "opponentContinued". I made dummyBoolean true as default. In "onContinueClicked" methods "dummyBoolean" is true, while "opponentContinued" is false.
public class MainActivity extends Activity {
private boolean opponentContinued;
private boolean dummyBoolean = true;
private void onOpponentContinued(){
opponentContinued = true;
dummyBoolean = opponentContinued;
}
private void onContinueClicked(){
// opponentContinued is false
// dummyBoolean is true
if (opponentContinued){
// Continue the game
}
}
}
I have no idea what could be the problem. Have anyone experienced a problem like this? Any help is appreciated.
EDIT
Please note that the problem has to do nothing with saving states during screen rotation. The problem is that the variable "opponentContined" is "magically" assigned to the default value false, after it is set to true.
I know this sounds unrealistic but it has ruined me a lot of time.