First Challenge


#1

Hi, I am having some trouble with fixing the first loophole. I am trying to do something similar to what was done in an earlier chapter with mCurrentIndex, but can’t seem to get it to work.

This is what I have so far in CheatActivity:

@Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); savedInstanceState.putString(KEY_INDEX, mAnswerTextView.toString()); }

I can’t figure out what code needs to go in onCreate(…)

…now I can get it work if I add this code at the end of onCreate(…), but it doesn’t feel right to me because it looks too similar to what’s in the mShowAnswer listener and seems redundant:

[code]if (savedInstanceState != null) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.true_button);
} else {
mAnswerTextView.setText(R.string.false_button);
}

	}[/code]

Any help would be greatly appreciated. Thanks.


#2

Hmm, here is what i did

add this function:

@Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); Log.i(TAG, "onSaveInstanceState"); savedInstanceState.putBoolean(IS_CHEATER, mCheater); }
add these 2 variables at the top private static final String IS_CHEATER = "false"; private static final String TAG = "CheatActivity";
add this after setContentView in onCreate mCheater = false; if (savedInstanceState != null) { mCheater = savedInstanceState.getBoolean(IS_CHEATER, false); }

Now it’s been a while since i did it and I changed some other stuff. Let me know if you find problems


#3

Looks good to me!


#4

After working on this challenge, a combination of RumHam and biamigueiss answers are needed to make this work.

There is duplication of code

package com.bignerdranch.android.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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.bignerrandch.android.geoquiz.answer_is_true";
	public static final String EXTRA_ANSWER_SHOWN = "com.bignerdranch.android.geoquiz.answer_shown";

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

	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.answer_text_view);

		mCheater = false;
		if (savedInstanceState != null) {
	        mCheater = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN, false);
			setAnswerShownResult(mCheater);

			if (mAnswerIsTrue) {
				mAnswerTextView.setText(R.string.true_button_text);
			}
			else {
				mAnswerTextView.setText(R.string.false_button_text);
			}
	    } else {
	    	setAnswerShownResult(false);
	    }

		mShowAnswer = (Button)findViewById(R.id.show_answer_button);
		mShowAnswer.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mAnswerIsTrue) {
					mAnswerTextView.setText(R.string.true_button_text);
				}
				else {
					mAnswerTextView.setText(R.string.false_button_text);
				}

				setAnswerShownResult(true);

				mCheater = true;
			}
		});
	}

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		super.onSaveInstanceState(savedInstanceState);
		savedInstanceState.putBoolean(EXTRA_ANSWER_SHOWN, mCheater);
	}

}

#5

Hi,

I have a similar solution that also works. But I don’t exactly understand why…

If if save mIsAnswerShown with something like this…

protected void onSaveInstanceState(Bundle outState) {
	super.onSaveInstanceState(outState);
	outState.putBoolean(EXTRA_ANSWER_SHOWN, mIsAnswerShown);
}

…and mIsAnswerShown was true, why doesn’t it stay true forever?
How does the system know if I created a new CheatActivity (reset mIsAnswerShown) or if I just rotated the device (keep mIsAnswerShown)?
Where and when does the savedInstanceState get “resetted” ?

Thanks. :geek:


#6

@atexit8

In your code you have wrote this.

if (mAnswerIsTrue) {
                mAnswerTextView.setText(R.string.true_button_text);
             }
             else {
                mAnswerTextView.setText(R.string.false_button_text);
             }
           } else {
              setAnswerShownResult(false);
           }

Why do you need the double else? If the mAnswerIsTrue is false then your code will continue at the first else, and then at the second? What’s the point? I am missing something?
I think that you just need one else.

if (mAswerIsTrue) {
    // commands }
else {
   // commands }

EDIT: Nevermind I found what you did. Just a little of bad writing.


#7

[quote=“NerdAkumu”]@atexit8

EDIT: Nevermind I found what you did. Just a little of bad writing.[/quote]

I am not sure what you meant by “bad writing”, but the second else goes with if (savedInstanceState != null) {.

if (savedInstanceState != null) { ... } else { ... }


#8

Yeah.I was looking your code in a Text Editor and the second else was a part of the second if.
Your Format:

          if (savedInstanceState != null) {
               mCheater = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN, false);
             setAnswerShownResult(mCheater);

             if (mAnswerIsTrue) {
                mAnswerTextView.setText(R.string.true_button_text);
             }
             else {
                mAnswerTextView.setText(R.string.false_button_text);
             }
           } else {
              setAnswerShownResult(false);
           }

What I thing is the right format:

          if (savedInstanceState != null) {
               mCheater = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN, false);
               setAnswerShownResult(mCheater);

             if (mAnswerIsTrue) {
                mAnswerTextView.setText(R.string.true_button_text);
             }
             else {
                mAnswerTextView.setText(R.string.false_button_text);
             }
          }
          else {
              setAnswerShownResult(false);
          }

Maybe I am the weird. Don’t mind me. :slight_smile:


#9

[quote=“NerdAkumu”]Yeah.I was looking your code in a Text Editor and the second else was a part of the second if.
Your Format:

          if (savedInstanceState != null) {
               mCheater = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN, false);
             setAnswerShownResult(mCheater);

             if (mAnswerIsTrue) {
                mAnswerTextView.setText(R.string.true_button_text);
             }
             else {
                mAnswerTextView.setText(R.string.false_button_text);
             }
           } else {
              setAnswerShownResult(false);
           }

What I thing is the right format:

          if (savedInstanceState != null) {
               mCheater = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN, false);
               setAnswerShownResult(mCheater);

             if (mAnswerIsTrue) {
                mAnswerTextView.setText(R.string.true_button_text);
             }
             else {
                mAnswerTextView.setText(R.string.false_button_text);
             }
          }
          else {
              setAnswerShownResult(false);
          }

Maybe I am the weird. Don’t mind me. :slight_smile:[/quote]

The “format” I am using is the format used by the authors of the book.
Reference Listing 2.10 page 43.
Though the first } else { should also be all on one line

This “format” is also used by Oracle in their documentation of Java.