CheatActivity Rotation Challenge


#1

I’m trying to do something similar to what we did in Chapter 3 but the cheating result still gets cleared out after rotation. This is what I have so far:

[code]package com.example.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 {

public static final String EXTRA_ANSWER_IS_TRUE = "com.example.android.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "com.example.android.geoquiz.answer_shown";

private static final String TAG = "CheatActivity";

private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
private boolean mCheatButtonWasPressed;

private void setAnswerShownResult(boolean isAnswerShown) {
	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);
	
	mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
	
	mAnswerTextView = (TextView)findViewById(R.id.answerTextView);
	
	  // Answer will not be shown until the user
      // presses the button
	mCheatButtonWasPressed = false;
	if (savedInstanceState != null) {
		mCheatButtonWasPressed = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN, false);
		setAnswerShownResult(mCheatButtonWasPressed);
	} else {
		setAnswerShownResult(false);
	}
	
	
	mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
	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);
			mCheatButtonWasPressed = true;
		}
	});
	
}
//Save value after rotation
@Override
   public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);
      Log.i(TAG, "onSaveInstanceState");
      savedInstanceState.putBoolean(EXTRA_ANSWER_SHOWN, mCheatButtonWasPressed);
   }

}
[/code]