In my android app there are 2 activities. I am navigating from activity1
to activity2
. I am returining to activity1
by pressing a button in activity2
. I want the activity1
to retain the previous state. I am using following code to achieve this.
This code to save the state.
@Override
public void onSaveInstanceState(Bundle savedInstanceState1) {
super.onSaveInstanceState(savedInstanceState1);
if (mode == 3) {
savedInstanceState1.putInt("totscore", totscore);
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
savedInstanceState1.putInt("ballmatrix" + i + "" + j, ballmatrix[i][j][2]);
}
}
}
}
This code to retrieve the state.
@Override
public void onRestoreInstanceState(Bundle savedInstanceState1) {
super.onRestoreInstanceState(savedInstanceState1);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
if (mode == 3 && restore == 1) {
Log.d("ok", "2");
if (savedInstanceState1 != null) {
Log.d("ok", "3");
totscore = savedInstanceState1.getInt("totscore");
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
ballmatrix[i][j][2] = savedInstanceState1.getInt("ballmatrix" + i + "" + j);
}
}
}
}
drawmatrix();
}
But when retrieving is done, savedInstanceState1 != null
turns out to be false everytime. What is wrong here ?