Solution to First Challenge


#1
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 {
  private static final String KEY_IS_CHEATER = "IS_CHEATER";
  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";

  boolean mAnswerIsTrue;
  boolean mIsCheater;

  TextView mAnswerTextView;
  Button mShowAnswer;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cheat);

    mAnswerTextView = (TextView) findViewById(R.id.answerTextView);

    mShowAnswer = (Button) findViewById(R.id.showAnswerButton);
    mShowAnswer.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        setAnswer();
        setAnswerShownResult(mIsCheater = true);
      }
    });

    mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
    if (savedInstanceState != null) {
      mIsCheater = savedInstanceState.getBoolean(KEY_IS_CHEATER, false);
      if (mIsCheater) {
        setAnswer();
      }
    }
    setAnswerShownResult(mIsCheater);
  }

  @Override
  protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putBoolean(KEY_IS_CHEATER, mIsCheater);
  }

  private void setAnswerShownResult(boolean isAnswerShown) {
    Intent data = new Intent();
    data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
    setResult(RESULT_OK, data);
  }

  private void setAnswer() {
    if (mAnswerIsTrue) {
      mAnswerTextView.setText(R.string.true_button);
    } else {
      mAnswerTextView.setText(R.string.false_button);
    }
  }
}

#2

That code works well, but I have a doubt. I think we could save on the bundle the mAnswerIsTrue field and get it from there, with that we don’t have to make a call to mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false); on the case that we got a bundle that is not null on onCreate(). So, the question is: the intent passed to the activity is saved automatically on configuration changes? It too would be nice to know too about the performance of the getIntent()… call and the saving and loading from the bundle in case of configuration changes.

This is my first post, new to android…


#3

Well, I really think that this solution is better, if at all because is clearer. What everyone think??

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 {
  private static final String KEY_IS_CHEATER = "IS_CHEATER";
  private static final String KEY_ANSWER_IS_TRUE = "ANSWER_IS_TRUE";
  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";

  boolean mAnswerIsTrue;
  boolean mIsCheater;

  TextView mAnswerTextView;
  Button mShowAnswer;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cheat);

    mAnswerTextView = (TextView) findViewById(R.id.answerTextView);

    mShowAnswer = (Button) findViewById(R.id.showAnswerButton);
    mShowAnswer.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        setAnswer();
        setAnswerShownResult(mIsCheater = true);
      }
    });

    if (savedInstanceState != null) {
      mAnswerIsTrue = savedInstanceState.getBoolean(KEY_ANSWER_IS_TRUE, false);
      mIsCheater = savedInstanceState.getBoolean(KEY_IS_CHEATER, false);
    } else {
      mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
      mIsCheater = false;
    }

    if (mIsCheater) { setAnswer(); }
    setAnswerShownResult(mIsCheater);
  }

  @Override
  protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putBoolean(KEY_IS_CHEATER, mIsCheater);
    savedInstanceState.putBoolean(KEY_ANSWER_IS_TRUE, mAnswerIsTrue);
  }

  private void setAnswerShownResult(boolean isAnswerShown) {
    Intent data = new Intent();
    data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
    setResult(RESULT_OK, data);
  }

  private void setAnswer() {
    if (mAnswerIsTrue) {
      mAnswerTextView.setText(R.string.true_button);
    } else {
      mAnswerTextView.setText(R.string.false_button);
    }
  }
}