1st Challenge


#1

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:


#2

What did you try? How did it break?


#3

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.


#4

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


#5

In CheatActivity


#6

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.


#7

Edit: Forget that. I made it :slight_smile:


#8

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?

Thanks.


#9

[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 =
		"com.bignerdranch.android.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN =
		"com.bignerdranch.android.geoquiz.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);
	
}

@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
	if (savedInstanceState != null) {
		setAnswerShownResult(mPushedButton);
	} 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);
			mPushedButton = true;
		}
	});
	
}

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

}
[/code]

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.


#10

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.