Found a small flaw in chapter 4

Found a small flaw in the logic after finishing Chapter 4: Debugging.

When we are finished with the app and we check whether we have something in our savedInstanceState or not we either go with restore or reset the game.

if (savedInstanceState != null) {
   score = savedInstanceState.getInt(SCORE_KEY)
   timeLeft = savedInstanceState.getInt(TIME_LEFT_KEY)
} else {

If you rotate the app before you even tap the button you will save the initial values. Time left as 60 and score as 0. And now when we have values in state we will continue with restoring the game which in turn will begin the countdown.

Maybe this isn’t the best solution but it solves the problem and is not more complicated.

First we add a new value to our companion object to be able to know if we started the game or not.


then instead of the if-case in onCreate we have this

score = savedInstanceState?.getInt(SCORE_KEY) ?: score
timeLeft = savedInstanceState?.getInt(TIME_LEFT_KEY) ?: timeLeft
gameStarted = savedInstanceState?.getBoolean(ONGOING_GAME_KEY) ?: gameStarted
if (gameStarted) restoreGame() else resetGame()

Hi cptfleppo,

I think you’ve done a great job figuring out a way to stop the countdown timer beginning before the button is tapped. You should be proud!

It’s enthused me to leave this in as a challenge for people to fix by themselves if they want to, just as you have.

Thank you from the bottom of my heart for reading through the book, I hope you’re finding it a valuable tool in learning everything Android!


Thank you! Yes this is a great challenge I think. Then you will use everything you learned from previous chapters.

I am actually already an iOS developer, been working a couple of years but going to learn some android now for fun :slight_smile: Especially when its 100% Kotlin which is very much appreciated!

I do like the book and it is a great read so far :raised_hands:

// KJ