package com.lucasleelz.geoquiz;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends AppCompatActivity {

    public static final String TAG = "QuizActivity";

    private Button mTrueButton;
    private Button mFalseButton;

    private TextView mQuestionTextView;

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

    private int mCurrentIndex = 0;

    private ImageButton mPreviousImageButton;
    private ImageButton mNextImageButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        setContentView(R.layout.activity_quiz);

        mTrueButton = (Button) findViewById(R.id.true_button);
        mFalseButton = (Button) findViewById(R.id.false_button);
        mPreviousImageButton = (ImageButton) findViewById(R.id.previous_image_button);
        mNextImageButton = (ImageButton) findViewById(R.id.next_image_button);

        mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
        mQuestionTextView.setOnClickListener(view -> checkNext());
        updateQuestion();

        mTrueButton.setOnClickListener(view -> checkAnswer(true));
        mFalseButton.setOnClickListener(view -> checkAnswer(false));
        mPreviousImageButton.setOnClickListener(view -> checkPrevious());
        mNextImageButton.setOnClickListener(view -> checkNext());
    }

    @Override
    protected void onStart() {
        super.onStart();

        Log.d(TAG, "onStart() called");
    }

    @Override
    protected void onResume() {
        super.onResume();

        Log.d(TAG, "onResume() called");
    }

    @Override
    protected void onPause() {
        super.onPause();

        Log.d(TAG, "onPause() called");
    }

    @Override
    protected void onStop() {
        super.onStop();

        Log.d(TAG, "onStop() called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        Log.d(TAG, "onDestroy() called");
    }

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

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

        int messageResId = userPressedTrue == answerIsTrue
                ? R.string.correct_toast
                : R.string.incorrect_toast;

        Toast.makeText(this, messageResId, Toast.LENGTH_LONG).show();
    }

    private void checkPrevious() {
        mCurrentIndex = mCurrentIndex == 0 ? mQuestionBank.length - 1 : mCurrentIndex - 1;
        updateQuestion();
    }

    private void checkNext() {
        mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
        updateQuestion();
    }
}

@@ -360,16 +360,68 @@
 tivity%22;
+%0A    public static final String KEY_INDEX = %22index%22;
 %0A%0A    pr
@@ -1270,24 +1270,146 @@
 ity_quiz);%0A%0A
+        if (savedInstanceState != null) %7B%0A            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);%0A        %7D%0A%0A
         mTru
@@ -2452,24 +2452,24 @@
 onPause();%0A%0A
-
         Log.
@@ -2495,32 +2495,326 @@
 alled%22);%0A    %7D%0A%0A
+    /**%0A     * %E5%9C%A8onStop%E4%B9%8B%E5%89%8D%E7%94%B1%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%E3%80%82%E9%99%A4%E9%9D%9E%E7%94%A8%E6%88%B7%E6%8C%89%E4%BA%86%E5%90%8E%E9%80%80%E9%94%AE%E3%80%82%0A     * @param outState%0A     */%0A    @Override%0A    protected void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A%0A        Log.i(TAG, %22onSaveInstanceState()%22);%0A%0A        outState.putInt(KEY_INDEX, mCurrentIndex);%0A    %7D%0A%0A
     @Override%0A  

保存数据支持设备旋转。