About the last Challenges

Hi everyone,

Could anyone please explain, for the less experienced ones like me, how to solve the last two challenges of the chapter?

I´ve been checking how other people solved it but I don´t fully understand what they do nor why.

Thank you very much I appreciate all comments.

can you tell me how to solve the second challenge? Thank you

For the first challenge, Preventing Repeat Answers, first add another field in the Question class:

private boolean mAlreadyAnswered;

this is initially set to false. Generate getter and setter methods for this field. When a question is answered, you will change it to true, like this (the line before the toast):

private void checkAnswer(boolean userPressedTrue){
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();

        int messageResId = 0;

        if (userPressedTrue == answerIsTrue){
            messageResId = R.string.toast_correct;
        } else {
            messageResId = R.string.toast_incorrect;


        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();

Then, using the Button.setEnabled() method, either enable or disable the buttons according to whether the question has been answered or not:


Put this code in updateQuestion() and checkAnswer() methods.

There are several ways to save the state of the Question array after rotation, but most likely you won’t be using any of them after you finish reading this book. You can pass isAlreadyAnswered boolean values for each Question in an array for example. I made Question class Serializable:

public class Question implements Serializable

put Question array in a bundle:

savedInstanceState.putSerializable("Questions", mQuestionBank);

and retrieve:

mQuestionBank = (Question[]) savedInstanceState.getSerializable("Questions");

I didn’t know about Serializable before, but it took me only a couple of minutes to find out how to do this.

For the second challenge, Graded Quiz, you need variables for the number of answers and correct answers in QuizActivity. In checkAnswer(), add code that increments the value of correctAnswers if the answer is correct, and code that increments the value of Answers every time the checkAnswer() method is called. If the number of answers equals the number of questions, show a toast with the result.