javaandroidoncreateonsaveinstancestate

How to use `onSaveInstanceState`?


What do I want to achieve?


What do I have?

  1. Probably correct onSaveInstanceState code that isn't working probably because of some @Override coming into conflict with it

What do I need?


Code

MainActivity.java (without onSaveInstanceState)

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private EditText textBox;
    private int z;
    public int score = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
        textBox = findViewById(R.id.editText);
        textBox.requestFocus();
        setActual();
    }

    private void setActual() {
        Random random = new Random();
        int x = random.nextInt(10) + 1; //you get a random number between 0 and 9 then add 1 to get 1 - 10, no need to use array..
        int y = random.nextInt(10) + 1;
        z = x * y;
        String hintStr = x + " × " + y;
        ((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
    }


            public void onCheck(View view) {

        int answer = Integer.parseInt(textBox.getText().toString());
        String res = "Incorrect";
        if (answer == z) {
            res = "Correct";
            score++;
            Log.d("TAG", "Correct."); // right answer
            textBox.setText("");
            setActual(); // let's regenerate values
        } else if (score == 0) {
            Log.d("TAG", "Incorrect."); // wrong answer
            textBox.setText("");
            setActual();
            ((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
        } else {
            Log.d("TAG", "Incorrect."); // wrong answer
            textBox.setText("");
            score--;
            setActual();
        }
        ((TextView) findViewById(R.id.scoreText)).setText("Score:" + ' ' + score);
        ((TextView) findViewById(R.id.result_text_view)).setText(res);
    }

}

MainActivity.java (with onSaveInstanceState)

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private EditText textBox;
    private int z;
    public int score = 0;
    private int x;
    private int y;

    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putInt("x", x);
        savedInstanceState.putInt("y", y);
        savedInstanceState.putInt("score", score);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textBox = findViewById(R.id.editText);
        textBox.requestFocus();
        setActual();
        if (savedInstanceState != null) {
            x = savedInstanceState.getInt("x");
            y = savedInstanceState.getInt("y");
            score = savedInstanceState.getInt("score");
        }
    }

    private void setActual() {
        Random random = new Random();
        x = random.nextInt(10) + 1; //you get a random number between 0 and 9 then add 1 to get 1 - 10, no need to use array..
        y = random.nextInt(10) + 1;
        z = x * y;
        String hintStr = x + " × " + y;
        ((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
    }


            public void onCheck(View view) {

        int answer = Integer.parseInt(textBox.getText().toString());
        String res = "Incorrect";
        if (answer == z) {
            res = "Correct";
            score++;
            Log.d("TAG", "Correct."); // right answer
            textBox.setText("");
            setActual(); // let's regenerate values
        } else if (score == 0) {
            Log.d("TAG", "Incorrect."); // wrong answer
            textBox.setText("");
            setActual();
            ((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
        } else {
            Log.d("TAG", "Incorrect."); // wrong answer
            textBox.setText("");
            score--;
            setActual();
        }
        ((TextView) findViewById(R.id.scoreText)).setText("Score:" + ' ' + score);
        ((TextView) findViewById(R.id.result_text_view)).setText(res);
    }

}


Solution

  • You should consider calling super.onSaveInstanceState()

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        // save your state
        super.onSaveInstanceState(outState);
    }
    

    Also, to avoid recreating the activity everytime the screen is rotated you could handle config change yourself by adding this to your Activity declaration in the app manifest :

    android:configChanges="keyboardHidden|orientation|screenSize"
    

    And override :

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
    

    It could save time for the user if your app doesn't need to be recreated after orientation or screenSize changes...