androidinstancesavestate

Why is onSaveInstanceState() not called after pressing "back" button?


I'm creating a bowling score application which will include a bingo game. To simplify, at the Score class, there is a button which will start a new activity (Bingo class). While in the game, for sure it is needed to go back and forth from Score to Bingo. The problem that I'm facing right now is the onSaveInstanceState is not called when I tried to "back" from Bingo to Score. Hence, the bingo numbers that should be fixed is changing every time I click button of "Game" which start the Bingo.

I had been searching and trying all of the related answers (including https://developer.android.com/guide/components/activities/activity-lifecycle), but nothing works for me. I also tried to temporarily remove the screenOrientation on any activity as I read that screen rotation will create a new instance. Here is my code that related to the problem.

Score.java (button to move the Bingo)

 @Override
    public void onClick(View view) {

        String button_text;
        button_text = ((Button) view).getText().toString();

        if (button_text.equals("GAME")) {
            Intent gamePage = new Intent(this, Game.class);
            startActivity(gamePage);
        }

Game.java (bingo1 and bingo2 is the array of integer)

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

        ...

        if (savedInstanceState != null){
            bingo1 = savedInstanceState.getIntArray("bingo1");
            bingo2 = savedInstanceState.getIntArray("bingo2");
        }else {

            ...


     }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {

        savedInstanceState.putIntArray("bingo1", bingo1);
        savedInstanceState.putIntArray("bingo2", bingo2);

        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {

        super.onRestoreInstanceState(savedInstanceState);


        bingo1 = savedInstanceState.getIntArray("array1");
        bingo2 = savedInstanceState.getIntArray("array2");
    }

Correct me if by calling in onCreate() and onRestoreInstanceState at the same time is wrong.

As stated earlier, I tried to have the bingo number fix throughout the back and forth movement from Score to Game/Bingo, which I failed to achieve by using these code. I don't know if there are more code which related to the problem as I'm still new, but it will be really helpful if you guys can help. Thank you!


Solution

  • I am assuming that Bingo and Score are both activities.

    The problem that I'm facing right now is the onSaveInstanceState is not called when I tried to "back" from Bingo to Score

    It is not supposed to be called then. Your Bingo activity instance is being permanently destroyed. It has no more instance state to save.

    If Bingo and Score are supposed to share common state, then perhaps they should not be separate activities. Instead, use one activity, two fragments (Bingo and Score), with a shared ViewModel between them.