1st Challenge


Yo. I got a problem with the 1st challenge… I tried to do something like the mCurrentIndex, but it doesn’t work :confused:

Can someone help me? :slight_smile:


What did you try? How did it break?


I tried to make it like I did with mCurrentIndex, saving data when I rotate the emulator. The problem is that, when I go the Cheat acitivity and press the Show Answer and then rotate then emulator and going back to QuizActivity I can still cheat without getting the cheating toast, just the correct/incorrect answers.


Where are you saving out the cheating value? In CheatActivity, or QuizActivity?


In CheatActivity


The tricky part to that challenge is making sure that the result value is set. After the user rotates, the value set with setResult() when the cheat button is tapped is discarded. So if the user rotates and taps the back button, they will not see that result - even if you saved it out properly in onSaveInstanceState.

See if that helps you see how to get it working. If not, post some code here and I can get more specific.


Edit: Forget that. I made it :slight_smile:


Regarding the value that was set with setResult() being discarded - could you please elaborate on this? Is it discarded after the rotation because it is an Intent associated with the activity, and the activity is restarted after the rotation?



[code]package com.bignerdranch.android.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 =
public static final String EXTRA_ANSWER_SHOWN =

private static final String TAG = "CheatActivity";

private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
private boolean mPushedButton;
private void setAnswerShownResult(boolean isAnswerShown) {
	Intent data = new Intent();
	data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
	setResult(RESULT_OK, data);

protected void onCreate(Bundle savedInstanceState) {
	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
	if (savedInstanceState != null) {
	} else {
	mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
	mShowAnswer.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			if (mAnswerIsTrue) {
			} else {
			mPushedButton = true;

// Save value after rotation	
public void onSaveInstanceState(Bundle savedInstanceState) {
	Log.i(TAG, "onSaveInstanceState");
	savedInstanceState.putBoolean(EXTRA_ANSWER_SHOWN, mPushedButton);


I have seen several posts tell how to do it, but instead I would like to know why my code is not working. If I initialize mPushedButton = true then it kinda works but you get the cheating is wrong toast whenever you go to the CheatActivity even if you don’t press the show answer button. If I don’t set mPushedButton I can’t get the value to pass across activities.


Nix my last post! I realized I had to call the value FROM savedInstanceState.getBoolean() not just pass the boolean variable as an argument to setAnswerShownResult.