Preventing Repeat Answers - Passing int array values in onSaveInstanceStat

My solution to the Preventing Repeat Answers challenge was to create an array:

private int[] disableIndex = new int[]{0,0,0,0,0,0};

I then created two new methods:

private void disable(int index){
    disableIndex[index] = 1;

private void checkEnabled(int index){
    if (disabledIndex[index] == 1){
    } else {

Every time a True or False button is pressed, I added a line in those methods that reads: disable(mCurrentIndex);

Every time a Next or Previous button is pressed, I added a line in those methods that reads: checkEnabled(mCurrentIndex);

Finally, in the onSaveInstanceState method, I added:
saveInstanceState.putIntArray(USED_ARRAY, disableIndex);

And in the check if savedInstanceState is null method, I added:
disableIndex = savedInstanceState.getIntArray(USED_ARRAY);

This words until I rotate the screen, at which point the app crashes. Am I not allowed to pass an integer array into the saved instance state?

you can do that,
I used a code for pass an integer array that into the saved Instance state.

public void onSaveInstanceState(Bundle savedInstanceState) {

        int[] questionsAnswered = new int[mQuestionsBank.length];
        for (int i = 0; i < mQuestionsBank.length; i++) {
            questionsAnswered[i] = mQuestionsBank[i].getAnswered();

        savedInstanceState.putIntArray(KEY_INDEX_QUESTIONS_ANSWERED, questionsAnswered);

Maybe it helps to you. And then

 if (savedInstanceState != null) {
            int[] questionAnswered = savedInstanceState.getIntArray(KEY_INDEX_QUESTIONS_ANSWERED);
            for (int i = 0; i < questionAnswered.length; i++) {

Thanks lscodex! That method had a cleaner approach overall. I think mine might have worked. I figured out the problem in the following code (using your version):

if(savedInstanceState != null){
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);

            int[] questionsAnswered = savedInstanceState.getIntArray(USED_ARRAY);
            for(int i = 0; i < questionsAnswered.length; i++){

For some reason, the checkEnabled() method call was the error. Once I moved it out of this method and into the main onCreate method, it worked.


the “For some reason” is that:
the checkEnabled() method call was the error because the reason is that you can call re-created method. You should not forget that Android views gets ready to destroy activity when maybe you change orientation or Android killed your application because of low memory.And Before the activity is destroyed, SaveInstanceState() gets called that saves the variables. So this bundle in saveInstanceState don’t know to the checkEnabled method because it re-creates. So you must call that checkEnabled absolutely in onCreate() .

That makes sense, thanks!