mchd
mchd

Reputation: 3163

OnSaveInstanceState does not save the instance

I'm my app's onSaveInstanceState implementation. I've successfully implemented my onSaveInstanceState but it does not maintain the current question at orientation change. The following is my MainActivity. I've been following the book BigNerdRanchGuide from page to page. I don't understand why it isn't working.

Thanks.

    public class MainActivity extends AppCompatActivity {

        public static final String TAG  = "QuizActivity";
        public static final String KEY_INDEX = "index";

        private TextView mTextView;
        private Button mTrueButton, mFalseButton;
        private ImageButton mNext, mPrev;

        private Question[] mQuestionBank = new Question[]{
                new Question((R.string.pacific_ocean), true),
                new Question((R.string.syria_europe), false),
                new Question((R.string.canada_na), true),
                new Question((R.string.africa_country), false),
                new Question((R.string.china_continent), false)
        };

        private int mCurrentIndex = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Log.d(TAG, "OnCreate(Bundle) called");

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

            mTextView = (TextView)findViewById(R.id.question);
            int question = mQuestionBank[mCurrentIndex].getTextResId();
            mTextView.setText(question);
...

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



        private void updateQuestion() {
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            int question = mQuestionBank[mCurrentIndex].getTextResId();
            mTextView.setText(question);
        }

        private void lastQuestion(){

            if (mCurrentIndex > 0){
                mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
                int question = mQuestionBank[mCurrentIndex].getTextResId();
                mTextView.setText(question);
            }else{
                Toast.makeText(this, "You're at the last question", Toast.LENGTH_SHORT).show();
                return;
            }
        }

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

            int messageResId = 0;

            if(userPressedTrue == answerIsTrue){
                messageResId = R.string.correct;
            }else{
                messageResId = R.string.incorrect;
            }
            Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
        }
    }

Upvotes: 1

Views: 286

Answers (1)

Iulian Popescu
Iulian Popescu

Reputation: 2643

Let's suppose that currentIndex=2. When you rotate the screen and activity is recreated the currentIndex will have the save value. The issue here is that you were setting the question 2 times when the activity will be created: first here

int question = mQuestionBank[mCurrentIndex].getTextResId();
mTextView.setText(question);

and the second time at the end of onCreate (which is not visible anymore) by calling updateQuestion(). So, the value 2 will be in the end a 3 resulting in this weird behaviour. A possible solution for this would be to delete the first piece of code from above, subtract one from the current index and make sure to set the value of current index to -1 (you might have notice that the first question displayed is actually the second one in the array).

Upvotes: 1

Related Questions