How to save a Button[] text in savedPreferences

54 Views Asked by At

I have Button[] buttons = new Button[9]; and later in the activity the buttons will be set to either X or O. I need to save whether a given button has its text as X or O. When a button is clicked on, that button will change its text. Since text is not predetermined or editText, I'm not sure how to save it so it can be restored when the activity needs to be destroyed to rotate the screen. Would using a loop for saving all 9 button's text be a thing?

@Override
    public void onClick(View v) {
        if (!((Button)v).getText().toString().equals("")) {
            return;
        }

        String buttonID = v.getResources().getResourceEntryName(v.getId());
        int gameStatePointer = Integer.parseInt(buttonID.substring(buttonID.length()-1));

        if (activePlayer) {
            ((Button)v).setText("X");
            ((Button)v).setTextColor(Color.parseColor("#ff4a4a"));
            gameState[gameStatePointer] = 0;
            gameStatus.setText("O's Turn");
        }
        else {
            ((Button)v).setText("O");
            ((Button)v).setTextColor(Color.parseColor("#70FFEA"));
            gameState[gameStatePointer] = 1;
            gameStatus.setText("X's Turn");
        }
2

There are 2 best solutions below

0
heX On BEST ANSWER

You will need to save the state of your view in the saved stated bundle in order to restore it after the screen rotates.

First, you need to represent the state of the buttons in some kind of simple structure. A quick solution would just be to create a comma separated list with values representing the button states from 0 to 8 (9 possible buttons). For example: "O,X,O,-,-,O,X,X,-".

After, you need to save your button states in:

@Override
protected void onSaveInstanceState(@NonNull final Bundle outState) {
    super.onSaveInstanceState(outState);
    final String screenState = // ... loop to generate screen state
    outState.putString("some_key", screenState);
}

And onCreate() you need to restore the screen if the value is present in the bundle:

@Override
public void onCreate(@Nullable final Bundle savedInstanceBundle) {
    if (bundle != null) {
        final String state = bundle.getString("some_key");
        if (state != null) {
            // ... restore state
        }
    }
}

Note: I agree with the previous answer, the screen state shouldn't be stored in the view in this fashion, but I just wanted to provide a simple answer without getting into the complexities of app architecture.

6
Gabe Sechan On

A loop saving all the button's texts could work. But really your problem is that this is not a good way to structure your data. You shouldn't be storing your state in your view. You should have a variable named board that holds the state of each square in your game, and use that to set your views. Then you just save the board variable on rotation.

In general, your view layer shouldn't be storing data. It should be set to display data stored elsewhere.