KEY_INDEX not saving mCurrentindex


#1

I’ve followed everything stated in the book but it seems that when I rotate the device the mCurrentIndex isn’t saved at all and so the wrong question appears.

I’m currently using Android Studio
This is my code:

[code]package com.example.matt95.geoquiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity {

private Button mTrueButton;
private Button mFalseButton;
private ImageButton mNextButton;
private TextView mQuestionTextView;
private TrueFalse[] mQuestionBank = new TrueFalse[]{new TrueFalse(R.string.question_oceans, true), new TrueFalse(R.string.question_mideast, false), new TrueFalse(R.string.question_africa, false), new TrueFalse(R.string.question_america, true), new TrueFalse(R.string.question_asia, true)};
private int mCurrentIndex = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState != null) {
        mCurrentIndex = savedInstanceState.getInt("index");
    }
    setContentView(R.layout.activity_quiz);

    mQuestionTextView = (TextView) findViewById(R.id.question_text);
    mQuestionTextView.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();
        }
    });

    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkAnswer(true);
        }
    });
    mFalseButton = (Button) findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkAnswer(false);
        }
    });
    mNextButton = (ImageButton) findViewById(R.id.next_button);
    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();
        }
    });
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt("index", mCurrentIndex);
}

private void checkAnswer(boolean userPressedTrue) {
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
    int messageResId = 0;
    if(userPressedTrue == answerIsTrue) {
        messageResId = R.string.correct_toast;
    } else
        messageResId = R.string.incorrect_toast;
    Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
}

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.geo_quiz, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}[/code]

What am I doing wrong?


#2

Try adding a call to updateQuestion() at the end of onCreate(). This will cause the current question to be displayed based on the saved value of mCurrentIndex. Right now you’re only calling updateQuestion() in the event handlers.


#3

That was it! Thanks for the suggestion!