The first challenge


#1

Hello, i have been looking to do the first challenge in this chapter for days now and I haven’t been able to find any solution for this. I have done all the previous challenges but this one is just too hard :confused: I looked around the forum and could not find any proper, detailed solution.

I have been trying to save the value of mCheater(a boolean variable i made to use for the setAnswerShown variable) using onSaveInstanceState but somehow, it just does not. I am, horribly, stuck. Here is my full piece of code and hopefully someone might be able to help. Any help would be appreciated. Thanks in advance :slight_smile:

[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";
public static final String EXTRA_ANSWER_SHOWN = "com.bidnerdranch.android.geoquiz";
private boolean mAnswerIsTrue;
private boolean mCheater = false;
private TextView mAnswerTextView;
private Button mShowAnswer;
private static final String KEY_CHEATVALUE = "cheatvalue";
private static final String TAG = "CheatActivity";

private void setAnswerShown(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);
	
	setAnswerShown(mCheater);
	
	mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
	mShowAnswer.setOnClickListener(new View.OnClickListener() {			
		@Override
		public void onClick(View v) {
			setAnswerShown(true);
			if(mAnswerIsTrue)
				mAnswerTextView.setText(R.string.true_button);
			else
				mAnswerTextView.setText(R.string.false_button);
		}
	});
	
	if(savedInstanceState != null) {
		mCheater = savedInstanceState.getBoolean(KEY_CHEATVALUE);
	}
            mCheater = false;

}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
	Log.i(TAG, "onSaveInstanceState");
	super.onSaveInstanceState(savedInstanceState);
	savedInstanceState.putBoolean(KEY_CHEATVALUE, mCheater);
}

}
[/code]


#2

I posted the CheatActivity.java that worked for me in the other thread entitled First Challenge.


#3

I saw that but your code seemed a bit confusing and i couldn’t really understand it, which was the reason I posted here


#4

I am not sure what you are wanting.
You posted the original code.

What do want us to do with it?
What have you attempted to make it work?


#5

[quote=“atexit8”]I am not sure what you are wanting.
You posted the original code.

What do want us to do with it?
What have you attempted to make it work?[/quote]

I have already specified it in my first post. Your code works great but i just can’t understand it properly, because i seem to have done most of the steps similarly but i don’t get the result properly. If you could, maybe, explain the changes you made for the challenge, it had be a great help as i would be able to pinpoint what i did wrong.


#6

You are doing everything right by saving the mCheater variable in the Bundle and then restoring it. You are just not updating the value of mCheater anywhere except by setting it to false at the end of onCreate()

What you need to do is update mCheater to true when the user presses the Show Answer button and remove setting it to false at the end of onCreate(). Your code is posted below with the fix:

mShowAnswer.setOnClickListener(new View.OnClickListener() {         
   @Override
   public void onClick(View v) {
      setAnswerShown(true);
      mCheater = true;
      if(mAnswerIsTrue)
         mAnswerTextView.setText(R.string.true_button);
      else
         mAnswerTextView.setText(R.string.false_button);
   }
});

if(savedInstanceState != null) {
	mCheater = savedInstanceState.getBoolean(KEY_CHEATVALUE);
}

Hope this helps!


#7

[quote=“quist50”]You are doing everything right by saving the mCheater variable in the Bundle and then restoring it. You are just not updating the value of mCheater anywhere except by setting it to false at the end of onCreate()

What you need to do is update mCheater to true when the user presses the Show Answer button and remove setting it to false at the end of onCreate(). Your code is posted below with the fix:

mShowAnswer.setOnClickListener(new View.OnClickListener() {         
   @Override
   public void onClick(View v) {
      setAnswerShown(true);
      mCheater = true;
      if(mAnswerIsTrue)
         mAnswerTextView.setText(R.string.true_button);
      else
         mAnswerTextView.setText(R.string.false_button);
   }
});

if(savedInstanceState != null) {
	mCheater = savedInstanceState.getBoolean(KEY_CHEATVALUE);
}

Hope this helps![/quote]

That helped a ton… Thanks a lot :slight_smile: