All challenges


#1

Right here’s my solution for the 3 challenges: -

For the 1st challenge, in CheatActivity used a boolean mCheatedRotation, set to true in setAnswerShownResult() and put into the bundle in onSaveInstaceState(). Retrieved it in onCreate().
For the 2nd & 3rd challenge, in QuizActivity used an array of booleans mQuestionWasCheated, tracking the index with mCurrentIndex.
put the value of mQuestionWasCheated[mCurrentIndex] into the bundle in onSaveInstaceState().
Retrieved it in onCreate() and used it in IF statement in checkAnswer().

Yes, i’ve been bad and put the onClick listeners in the onResume() and other changes :smiley:

All the code below and hope it makes sense? :question:

QuizActivity.java

package uk.co.zawaung.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity {

	private Button mTrueButton, mFalseButton, mNextButton, mCheatButton;
	private TextView mQuestionTextView;
	private static final String TAG = "QuizActivity";
	private static final String KEY_INDEX = "index";
	private static final String HAS_CHEATED = "has cheated";
	
	// Setup an array to hold TrueFalse's
	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_americas, true),
			new TrueFalse(R.string.question_asia, true) };

	private boolean[] mQuestionWasCheated = new boolean[mQuestionBank.length];
	
	// index of TrueFalse array
	private int mCurrentIndex = 0;

	// Iterate the question
	private void updateQuestion() {
		//Log.e(TAG, "Updating question text for question #" + mCurrentIndex, new Exception());
		int question = mQuestionBank[mCurrentIndex].getQuestion();
		mQuestionTextView.setText(question);
	}

	// Check the question and display the correct toast
	private void checkAnswer(boolean userPressedTrue) {

		// GEt the answer to the question
		boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
		
		if (mQuestionWasCheated[mCurrentIndex]){
			if (userPressedTrue == answerIsTrue) {
				Toast.makeText(this, R.string.judgement_toast, Toast.LENGTH_SHORT)
				.show();
			} else {
				Toast.makeText(this, R.string.judgement_incorrect_toast, Toast.LENGTH_SHORT)
						.show();
			}
		} else {
			if (userPressedTrue == answerIsTrue) {
				Toast.makeText(this, R.string.correct_toast, Toast.LENGTH_SHORT)
						.show();
			} else {
				Toast.makeText(this, R.string.incorrect_toast, Toast.LENGTH_SHORT)
						.show();
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_quiz);
		Log.d(TAG, "onCreate(Bundle) called");

		// Check if index exists in the Bundle
		if (savedInstanceState != null) {
			mCurrentIndex = savedInstanceState.getInt(KEY_INDEX);
			mQuestionWasCheated[mCurrentIndex] = savedInstanceState.getBoolean(HAS_CHEATED);
		}
		
		// Find the buttons
		mTrueButton = (Button) findViewById(R.id.true_button);
		mFalseButton = (Button) findViewById(R.id.false_button);
		mNextButton = (Button) findViewById(R.id.next_button);
		mCheatButton = (Button) findViewById(R.id.cheat_button);

		// Find the TextView
		mQuestionTextView = (TextView) findViewById(R.id.question_text_view);

		updateQuestion();
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "onResume() called");

		updateQuestion();

		// Set the onClick listener for the 'true' button
		mTrueButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				checkAnswer(true);
			}
		});

		// Set the onClick listener for the 'false' button
		mFalseButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				checkAnswer(false);
			}
		});

		// Set the onClick listener for the 'Next' button
		mNextButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
				Log.d(TAG, "mCurrentIndex: " + mCurrentIndex);
				//mIsCheater = false;
				updateQuestion();
			}
		});
		
		// Set the onClick listener for the 'Cheat' button
		mCheatButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent i = new Intent(QuizActivity.this, CheatActivity.class);
				i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, mQuestionBank[mCurrentIndex].isTrueQuestion());
				startActivityForResult(i, 0);
				
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		if (data == null){
			return;
		}
		mQuestionWasCheated[mCurrentIndex] = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);
	}

	@Override
	protected void onStart() {

		super.onStart();
		Log.d(TAG, "onStart() 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");
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {

		super.onSaveInstanceState(outState);
		Log.i(TAG, "onSaveInstanceState() called");

		outState.putInt(KEY_INDEX, mCurrentIndex);
		outState.putBoolean(HAS_CHEATED, mQuestionWasCheated[mCurrentIndex]);

	}
}

CheatActivity

package uk.co.zawaung.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends Activity {

	private boolean mAnswerIsTrue, mCheatedRotation = false;
	private Button mShowAnswer;
	private TextView mAnswerTextView;

	public static final String TAG = "CheatActivity";
	public static final String EXTRA_ANSWER_IS_TRUE = "uk.co.zawaung.geoquiz.answer_is_true";
	public static final String EXTRA_ANSWER_SHOWN = "uk.co.zawaung.geoquiz.answer_shown";
	private static final String CHEATED_AFTER_ROTATION = "uk.co.zawaung.geoquiz.cheated_after_rotation";

	private void setAnswerShownResult(boolean isAnswerShown) {
		mCheatedRotation = true;
		Intent data = new Intent();
		data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
		setResult(RESULT_OK, data);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cheat);
		
		if (savedInstanceState != null) {
			mCheatedRotation = savedInstanceState
					.getBoolean(CHEATED_AFTER_ROTATION);
		}

		Log.d(TAG, "onCreate() called");
		Log.d(TAG, "value of mAnswerTrue in onCreate(): " + mAnswerIsTrue);
		Log.d(TAG, "value of mCheatedRotation in onCreate(): " + mCheatedRotation);

		// Don't show the answer, until button is pressed
		setAnswerShownResult(false);


		// Get the EXTRA_ANSWER_IS_TRUE from the received intent
		mAnswerIsTrue = getIntent()
				.getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

		// get the button and textview;
		mShowAnswer = (Button) findViewById(R.id.showAnswerButton);
		mAnswerTextView = (TextView) findViewById(R.id.answerTextView);

	}

	@Override
	protected void onResume() {
		super.onResume();
		mShowAnswer.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (mAnswerIsTrue) {
					mAnswerTextView.setText(R.string.true_button);
				} else {
					mAnswerTextView.setText(R.string.false_button);
				}
				setAnswerShownResult(true);
			}
		});
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putBoolean(CHEATED_AFTER_ROTATION, mCheatedRotation);
		Log.d(TAG, "value of mCheatedRotation in onSaveInstanceState(): " + mCheatedRotation);
		
	}

}