简体   繁体   中英

Instance variable gets magically assigned in java

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.

https://developer.android.com/guide/topics/resources/runtime-changes.html

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android restarts the running Activity (onDestroy() is called, followed by onCreate()). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.

To properly handle a restart, it is important that your activity restores its previous state through the normal Activity lifecycle, in which Android calls onSaveInstanceState() before it destroys your activity so that you can save data about the application state. You can then restore the state during onCreate() or onRestoreInstanceState()

Your problem is that when the user rotates the device, your activity object is destroyed and a new one is created in its place. Thus the values are (re-)initialized during the creation of the new instance. You need to save the state and then restore it when the device is rotated.

When you are checking if the opponent continued, you should at first ask for a variable content. If opponentContinued is false, repeat the question to the opponent's device.

Default value of boolean in java is false since variable is being recreated assigned by default value false

The problem is Activity is getting recreated on screen orientation changed.you can solve this by 3 ways

1.you can make use of onSaveInstanceState and onRestoreInstanceState to save and retrieve boolean value

2.make your boolean variable static and declare it outside lifecycle methods

3.use shared preferences to keep track of your boolean variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM