I am following Big Nerd Ranch guide Android in which I am building a basic application which displays a set of questions and you say true or false. There is a button 'Cheat
' where in you can cheat. If you hit the button Cheat
see the answer, come back and type the correct answer then there is a toast
displayed 'Cheating is wrong'. This works fine in vertical screen but lets say I rotate the screen to horizontal, I view the answer and come back to horizontal, I can cheat. I dont want that to happen.
My Effort:
using onSavedInstanceState
to override the existing function to share values between screens & keeping track of the value in onCreate
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG, "onSaveInstanceState");
savedInstanceState.putBoolean(EXTRA_ANSWER_SHOWN, mAnswerIsTrue) ;
}
In onCreate
method,
if(savedInstanceState !=null)
{
mAnswerIsTrue = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN);
setAnswerShownResult(mAnswerIsTrue);
}
Here is my code:
package com.android.geoquiz;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
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
{
//Extras
public static final String EXTRA_ANSWER_IS_TRUE = "com.android.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "com.android.geoquiz.answer_shown";
private static final String TAG="CheatActivity";
private static final String KEY_INDEX = "cheat";
private boolean mAnswerIsTrue;
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.showAnswerButton);
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);
}
});
if(savedInstanceState !=null)
{
mAnswerIsTrue = savedInstanceState.getBoolean(KEY_INDEX, false);
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG, "onSaveInstanceState");
savedInstanceState.putBoolean(KEY_INDEX, mAnswerIsTrue) ;
}
@Override
public void onStart()
{
super.onStart();
Log.d(TAG, "onStart() called");
}
@Override
public void onPause()
{
super.onPause();
Log.d(TAG, "onPause() called");
}
@Override
public void onResume()
{
super.onResume();
Log.d(TAG, "onResume() called");
}
@Override
public void onStop()
{
super.onStop();
Log.d(TAG, "onStop() called");
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.d(TAG, "onDestroy() called");
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
}