2nd challenge question


#1

How do we solve the issue of screen rotation clearing out mIsCheater? I am thinking I have to modify savedInstanceState in onCreate() like we did for mCurrentIndex but don’t exactly know how to go about it. Help would greatly be appreciated.


#2

I solved the challenge with the same pattern that is used in mCurrentIndex. So i declared a variable of type boolean that indicates if is cheating, you must save the value of that variable the same way you did in mCurrentIndex and restore do the same to restore that value. Remember that Bundle do store multiple values (or Objects), not only one int (mCurrentIndex). So a hit for you:

if(savedInstanceState != null){ mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0); mIsCheater = savedInstanceState.getBoolean(CHEATING); }


#3

Awesome! I’ll try that now. Thank you!


#4

Ya thats good one
even i got the same idea and implemented :slight_smile:


#5

Hi everyone.

I have problem with this challenge.

I have added key:

Added mIsCheater line in checking:

if (savedInstanceState != null) { mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0); mIsCheater = savedInstanceState.getBoolean(KEY_CHEATER); }

Override method onSaveInstanceState:

@Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); //Log.i(TAG, "onSaveInstanceState"); savedInstanceState.putInt(KEY_INDEX, mCurrentIndex); savedInstanceState.putBoolean(KEY_CHEATER, mIsCheater); }

Where is my mistake?

QuizActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


public class QuizActivity extends Activity {

    //private static final String TAG = "QuizActivity";

    private static final String KEY_INDEX = "index";
    private static final String KEY_CHEATER = "cheater";

    private Button mTrueBtn;
    private Button mFalseBtn;
    private Button mCheatBtn;
    private ImageButton mNextBtn;
    private ImageButton mPrevBtn;

    private TextView mQuestionTextView;

    private boolean mIsCheater;

    private TrueFalse[] mQuestionBank = new TrueFalse[] {
            new TrueFalse(R.string.question_oceans, true),
            new TrueFalse(R.string.question_mideast, false),
            new TrueFalse(R.string.question_africa, false),
            new TrueFalse(R.string.question_americas, true),
            new TrueFalse(R.string.question_asia, true)
    };

    private int mCurrentIndex = 0;

    private void updateQuestion() {
        int question = mQuestionBank[mCurrentIndex].getQuestion();
        mQuestionTextView.setText(question);
    }

    private void checkAnswer(boolean userPressedTrue) {
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();

        int messageResId = 0;

        if (mIsCheater) {
            messageResId = R.string.judgment_toast;
        } else {
            if (userPressedTrue == answerIsTrue) {
                messageResId = R.string.correct_toast;
            } else {
                messageResId = R.string.incorrect_toast;
            }
        }
        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
    }

    private void nextQuestion() {
        mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
        updateQuestion();

        //было
        //mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
        //updateQuestion();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Log.d(TAG, "onCreate() called");
        setContentView(R.layout.activity_quiz);

        mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
        mQuestionTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextQuestion();
            }
        });

        mTrueBtn = (Button)findViewById(R.id.true_btn);
        mTrueBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
            }
        });

        mFalseBtn = (Button)findViewById(R.id.false_btn);
        mFalseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(false);
            }
        });

        mCheatBtn = (Button)findViewById(R.id.cheat_btn);
        mCheatBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(QuizActivity.this, CheatActivity.class);
                boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
                intent.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answerIsTrue);
                //startActivity(intent);
                startActivityForResult(intent, 0);
            }
        });

        mNextBtn = (ImageButton)findViewById(R.id.next_btn);
        mNextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextQuestion();
            }
        });

        mPrevBtn = (ImageButton)findViewById(R.id.prev_btn);
        mPrevBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + (mQuestionBank.length - 1)) % mQuestionBank.length;
                updateQuestion();
            }
        });

        if (savedInstanceState != null) {
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
            mIsCheater = savedInstanceState.getBoolean(KEY_CHEATER);
        }

        mIsCheater = false;
        updateQuestion();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) {
            return;
        }
        mIsCheater = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        //Log.i(TAG, "onSaveInstanceState");
        savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
        savedInstanceState.putBoolean(KEY_CHEATER, mIsCheater);
    }

    @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 boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

#6

[quote=“Kolonist”]Hi everyone.

I have problem with this challenge.

[/quote]

What problem are you having?
What is your code doing?


#7

[quote]What problem are you having?
What is your code doing?
[/quote]

So the task of second challenge not working. User could reset mIsCheater flag by changing rotation.


#8

I think you must delete

from onCreate().
It must be in mNextButton.setOnClickListener()