'Android activity loses data when switching between apps

I'm writing my first Android app. Essentially it shows and controls the score of a straight pool match. I've read articles about the lifecycle of an activity but I still have a problem I don't understand:

When I switch back to my app with a running game (so a score different from the initial 0:0 is shown) the activity sometimes loses its state and shows 0:0 instead of the score when I left the app. I overloaded the methods onSaveInstanceState and onRestoreInstanceState. The former get's called when I press the home button of my device. The latter never gets called. I've read that the method only gets called when onCreate is called. The onCreate method doesn't get called although the app needs longer than usual to reload after switching to some other apps in between. So I think the activity does get rebuild but obviously not by onCreate and the saved score is not loaded by onRestoreInstanceState.

Can you explain me what's happening and how to achieve the desired behaviour? Thank you very much!

edit: I was asked to post my onCreate() and onSaveInstanceState() methods. I tried to shorten them in a useful way. Please tell me If there is anything unclear or missing.

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

        final GameSetting gameSetting = getGameSettings();
        final GameData gameData = new GameData(gameSetting.getLeague());
        game = new GameLogic(gameData);

        scoreViews.put(PlayerId.PLAYER_A, (TextView) findViewById(R.id.playerAScore));
        
    }
    @Override
    protected void onSaveInstanceState(@NonNull final Bundle outState) {
        GameDataInstanceSupplier.saveInstance(game.getGameData(), outState);
        // inside the method the data gets saved like
        // outState.putInt(STATE_SCORE_A, data.getScore(PlayerId.PLAYER_A));

        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onRestoreInstanceState(@NonNull final Bundle inState) {
        super.onRestoreInstanceState(inState);
        GameDataInstanceSupplier.restoreInstance(game.getGameData(), inState);
        // inside the method the data gets loaded like
        // data.setScore(PlayerId.PLAYER_A, inState.getInt(STATE_SCORE_A));
    }

I put a "debug output" inside the onCreate() method and it did not appear in the debug log. The same was true for onRestoreInstanceState(). A message was printed inside onSaveInstanceState() when I pushed the home button.



Solution 1:[1]

I think your app is not the problem, you see, I came to find this page because I have the same problem switching aplications in my android phone, it's specially noticiable when usig 2 factor authentication and have to switch to text message, mail or other app to get the sent code, when switch back to the code input screen on the browser or app (teams for example) it is no longer there anymore and it has return to the begining, the login screen for example. This is very annoying because I have disable every thing that has to do with energy savings but still have the issue. I discovered that if I open first the message app and then do the authentication while switching very quickly just to see the code in 1 or 2 seconds and then return sometimes it works. It could be memory issue, or may be an unfulfilled politic on the energy savings. I think is probably the last because other explicitly stated properties like setting the screen brightness automatic adjust off and set to maximun works for a while and then returns to a default. It may even be a planned obsolescence. My phone is a Huawei P smart 2019 (POT-LX3) with android 10. I'll may be try a factory reset. Any suggestions are welcome.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Benjamin Penaloza