Challenge one and two JAVA

Hi, This my solution in Java. I have two problems:

  1. On Start App the first question does not show up. Even the buttons are locked after hit the answer I can loop Question list many time. How to prevent it e.g lock the list once finished last question?
  2. After last question the final Toast with % score does not show up.
    Any suggestions are welcomed.
    Thanks

public class MainActivity extends AppCompatActivity {

private static final String TAG = "QuizActivity";
private static final String KEY_INDEX = "index";
private int countCorrectAnswers = 0;
private int mCurrentIndex = 0;
private List<Integer> lockedQuestions = new ArrayList<>();
private Button mTrueButton;
private Button mFalseButton;
private ImageButton mNextButton;
private ImageButton mBackButton;
private TextView mQuestionTextView;

private Question[] mQuestionBank = new Question[]{
        new Question(R.string.question_africa, false),
        new Question(R.string.question_americas, true),
        new Question(R.string.question_mideast, true),
        new Question(R.string.question_oceans, true),
        new Question(R.string.question_poland, true),
        new Question(R.string.question_asia, false),
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "Method call: onCreate(Bundle)");
    setContentView(R.layout.activity_main);    

    if(savedInstanceState != null){
        mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
    }

    mQuestionTextView = (TextView) findViewById(R.id.question_text_view);

    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            lockedQuestions.add(mCurrentIndex);
            mTrueButton.setEnabled(false);
            mFalseButton.setEnabled(false);
            checkAnswer(true);
        }
    });
    mFalseButton = (Button) findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            lockedQuestions.add(mCurrentIndex);
            mTrueButton.setEnabled(false);
            mFalseButton.setEnabled(false);
            checkAnswer(true);
        }
    });

    mNextButton = (ImageButton) findViewById(R.id.next_button);
    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                isAnswered(mCurrentIndex);
                updateQuestion();
        }
    });

    mBackButton = (ImageButton) findViewById(R.id.back_button);
    mBackButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentIndex = mCurrentIndex - 1;
            if(mCurrentIndex == -1) {
                mCurrentIndex = 0;
            }
            isAnswered(mCurrentIndex);
            updateQuestion();
        }
    });
}

private void isAnswered(int index){
    if(mQuestionBank[index].isAnswered()){
        mTrueButton.setEnabled(false);
        mFalseButton.setEnabled(false);
    }else {
        mTrueButton.setEnabled(true);
        mFalseButton.setEnabled(true);
    }
}

private void updateQuestion() {
    int question = mQuestionBank[mCurrentIndex].getTextResId();
    mQuestionTextView.setText(question);
}

private void checkAnswer(boolean userPressedTrue){
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
    int messageResId = 0;
    if(userPressedTrue == answerIsTrue){
        messageResId = R.string.correct_toast;
        countCorrectAnswers++;
    }else {
        messageResId = R.string.incorrect_toast;
    }
    if(mQuestionBank.length == lockedQuestions.size()){
        String message = percentOfMarks(countCorrectAnswers);
        Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
    }else {
        mTrueButton.isEnabled();
        mFalseButton.isEnabled();
        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
    }
}

private String percentOfMarks(int numberOfCorrectAnswers){
    double per = numberOfCorrectAnswers * 100 / mQuestionBank.length;
    String message = "You got " + per + " % of the marks";
    return message;
}

@Override
protected void onStart() {
    super.onStart();
    Log.d(TAG, "Method call: onStart()");
}

@Override
protected void onStop() {
    super.onStop();
    Log.d(TAG, "Method call: onStop()");
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "Method call: onDestroy()");
}

@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG, "Method call: onResume()");
}

@Override
protected void onPause() {
    super.onPause();
    Log.d(TAG, "Method call: onPause()");
}

@Override
protected void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    Log.i(TAG, "onSaveInstanceState");
    savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
}

}

@michal could you also show us your Question class i.e. Question.java

In the method isAnswered(int index) you make the following call:-

if(mQuestionBank[index].isAnswered()){
 // Just some cool code
} else {
 // Yaaay!!! more cool code
}

But looking at your Question constructor it takes only two arguments the question itself and it’s answer. What about the 3rd flag which would indicate whether a user has answered a question or not?

Lastly you might need to save the list of lockedQuestions across a device configuration change like rotation the same way you have done it with mCurrentIndex.

If you’re still having issues just post a stacktrace here and the folks will help with debugging.