androidscreen-rotationonsaveinstancestateonrestoreinstancestate

state of app after 2 rotations


i have been through this documentation regarding the topic save a state before the foreground activity will be destroyed...

and everything works really good now (after a device rotation), but when i rotate my device again after a rotation, i will loose my data again :(

here is my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final MainActivity activity = this;
    activity.setTitle("Cow Counter");

    TextView QntyResultField = findViewById(R.id.textView);
    QntyResultField.setText(Integer.toString(cowQnty));
}

// invoked when the activity may be temporarily destroyed, save the instance state here
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("qnty", cowQnty);
}

// How we retrieve the data after app crash...
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    //cowQnty = savedInstanceState.getInt("qnty");

    TextView QntyResultField = findViewById(R.id.textView);
    QntyResultField.setText("Cows: "+Integer.toString(savedInstanceState.getInt("qnty")));
}

I think the solution will be maybe to implement a check if an instance state was already restored before...

i have tried then this here:

if(savedInstanceState.getInt("qnty") != 0){
    TextView QntyResultField = findViewById(R.id.textView);
    QntyResultField.setText("Cows: "+Integer.toString(savedInstanceState.getInt("qnty")));
}

buit then my inital part in my onCreate() method will write a zero in my result field

TextView QntyResultField = findViewById(R.id.textView);
QntyResultField.setText(Integer.toString(cowQnty));

Could anyone tell me if I am close to the solution?


Solution

  • You use a variable called cowQnty to store the value that is then saved in the bundle for your onSaveInstanceState as outState.putInt("qnty", cowQnty);, then when you restore it in onRestoreInstanceState you only set the TextView's value to the retrieved value and do not update the value for cowQnty.

    How do you expect then to save an empty field again? There are two solutions to this;

    Firstly, if cowQnty is not a sizeable amount and you do not mind using a tad of RAM, make cowQnty a static field and it will persist the data without needing to save it in a Bundle at all.

    Secondly, just set cowQnty's value once again when you restore your state (why did you comment it out??), like so:

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        cowQnty = savedInstanceState.getInt("qnty");
    
        TextView QntyResultField = findViewById(R.id.textView);
        QntyResultField.setText("Cows: "+Integer.toString(savedInstanceState.getInt("qnty")));
    }