Challenge fix the screen rotation "bug"


#1

I’m not sure whether this should be here or in the next chapter, but when I did the Challenge exercises, the app no longer reverts to the first question upon rotation. I’m good with that, but am not understanding what caused the change.

My guess is that is my solution to the problem of the Prev button crashing the app when you were already on the first question.
This is what I used:
mPreviousButton = (Button) findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + mQuestionBank.length - 1) % mQuestionBank.length;
updateQuestion();
}
});
Q1: Is there a “cleaner” way to prevent the negative index result?
Q2: Could this have fixed the rotation “bug”?

Any other code I should post?

Thanks.


#2

Your solution looks fine, and the behavior you’re seeing is expected. That’s what the code in chapter 3 is intended to do - to make it so that rotating the screen does not revert to the first question.

Listings 3.5, 6, and 7 are all responsible for making that happen. If you comment out those lines, it will revert to the first question again.

Check out the discussion around those lines for more information. If it’s still unclear, come back here and I’ll see if I can’t explain what’s going on to your satisfaction.


#3

I’m not sure if I’m not explaining myself well, or if I’m just not understanding well.

What I’m trying to say: I fixed the screen rotation “bug” BEFORE reading chapter 3 or inputting any of the code from Chapter 3. It was fixed while I was doing the Challenge exercises in Chapter 2. That is what is “confusing” me - the “bug” went away before the “fix” was made.

Also, is my formula for fixing the Prev button good, bad or indifferent?

Thanks.