First Challenge : Closing loophole (Simple solution I guess)

hello hello…
maybe I made the simple solution that works…

I set a default boolean value (false)

private var cheaterStatus = false

and used it inside the onClickListiner to change it to true in case the button was clicked

 showAnswerButton.setOnClickListener {
        val answerText = when{
            answerIsTrue -> R.string.true_button
            else -> R.string.false_button
        }
        answerTextView.setText(answerText)
        //create a function to return the result to MainActivity
        setAnswerShownResult(true)
        cheaterStatus = true
    }

Then i saved the result by overriding the onSave… function

 override fun onSaveInstanceState(savedInstanceState: Bundle) {
        super.onSaveInstanceState(savedInstanceState)
        Log.d(BOOLEAN_KEY, "onSave called.")
        savedInstanceState.putBoolean(BOOLEAN_KEY,cheaterStatus)
    }

and obviously retrieved it onCreate…

   //saving the status on configuration change.
        val cheatStatus = savedInstanceState?.getBoolean(BOOLEAN_KEY, false) ?: false
        setAnswerShownResult(cheatStatus)

and re-displayed the text of the answer if the retrieved boolean was true

  //if answer is true, show the text again
        if (cheatStatus) {
            val answerText = when {
                answerIsTrue -> R.string.true_button
                else -> R.string.false_button
            }
            answerTextView.setText(answerText)
        }

IT WORKED!

1 Like

The only problem I have is that when I rotate the device, the boolean text value dissapears but its still there

can’t get you… how did you saved it?

I saved it as a Boolean

share your code…
after you get the boolean from the savedInstanceState you should reassign local values with it and display the message again using the when

Okay give me some time I will do that