Very Simple and Compact Solution to Challenge 3


#1

First, create a constant that will act as a key for the list of cheated questions (just like we did for KEY_INDEX)

Now, make this Integer ArrayList which will store all the numbers which we have cheated on:

Now, add this function which we use when we press the Next Button (and Previous Button if you did the previous few challenges) to set mIsCheater by checking mCheat to see if it has mCurrentIndex

private void cheatFromList() { if (mCheat.contains(mCurrentIndex)) { mIsCheater = true; } else { mIsCheater = false; } }

After that, go to the onActivityResult function and add these lines of code which will add mCurrentIndex to mCheat if the user cheated

if (mCheat.contains(mCurrentIndex) == false && mIsCheater == true) { mCheat.add(mCurrentIndex); }

Then, go to the next (and previous button if you did the previous challenges) button(s) and add this line before the updateQuestion() line

Now, go to if (savedInstanceState != null) { and add this line of code, which you should know what it does if you’ve gotten this far…

Finally, go to onSaveInstanceState(Bundle savedInstanceState) and add savedInstanceState.putIntegerArrayList(CHEAT_LIST, mCheat);

And you’re done! :slight_smile:


#2

Nice solution but there is a bug.
User selects to cheat and then selects to see the answer .After going through all questions again to the question he saw the answer if he selects to cheat but he doesnt select to show the answer then it will appear that the user is not a cheater.


protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        if(data == null) {
               return ;
        }
        mIsCheater = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN,false);
            // add cheater to the list
        if (!mCheat.contains(mCurrentIndex) && mIsCheater == true) {
               mCheat.add(mCurrentIndex);
        }
             // check if he has cheated in the previous round
        else if(mIsCheater == false && mCheat.contains(mCurrentIndex)) {
               mIsCheater = true;
        }
    }