0

i'm new to android and am trying to save a boolean value when the screen rotates, basically my problem is current : when user presses "cheat" button in main activity he is redirected to a new activity, there he has a button "show answer" when he clicks on it, a boolean variable mIsCheater is set to true, but when the screen rotates the value becomes false.

Here is the code:

package com.example.macbookair.androidbooktutorials;

import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.macbookair.androidbooktutorials.Models.TrueFalse;

public class MainActivity extends AppCompatActivity {

    private static final String KEY_INDEX = "index";
    private static final String BOOLEAN_INDEX = "index";

    private Button mTrueButton,mFalseButton,mCheatButton;
    private ImageButton mNextButton;
    private ImageButton mPrevButton;
    private TextView mQuestionTextView;
    private int mCurrentIndex = 0;
    private boolean mIsCheater;

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





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mCheatButton = (Button) findViewById(R.id.cheat_button);

        if(savedInstanceState != null) {
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX,0);
            boolean theValue = savedInstanceState.getBoolean(BOOLEAN_INDEX);
            Log.d("Is that true? -> ", mIsCheater + " ");
        }


        mTrueButton = (Button) findViewById(R.id.true_button);
        mFalseButton = (Button) findViewById(R.id.false_button);


        mQuestionTextView = (TextView) findViewById(R.id.question_text_view);



        mNextButton = (ImageButton) findViewById(R.id.button_next);
        mPrevButton = (ImageButton) findViewById(R.id.button_prev);

        updateQuestion();

        mCheatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, CheatActivity.class);
                boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
                i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE,answerIsTrue);
//                startActivity(i);
                startActivityForResult(i,0);
            }
        });

        mPrevButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurrentIndex > 0) {
                    mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
                    updateQuestion();
                } else {
                    Toast.makeText(MainActivity.this,R.string.message,Toast.LENGTH_SHORT).show();
                }
            }
        });

        mQuestionTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex +1) % mQuestionBank.length;
                updateQuestion();
            }
        });

        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex +1) % mQuestionBank.length;
                mIsCheater = false;
                updateQuestion();
            }
        });

        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkedAnswer(true);
            }
        });

        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkedAnswer(false);
            }
        });
    }

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

    private void checkedAnswer(boolean isTrue) {
        boolean answer = mQuestionBank[mCurrentIndex].isTrueQuestion();

        int messageId = 0;

        if(mIsCheater) {
            messageId = R.string.judgment_toast;
        }

        if(isTrue == answer) {
            messageId = R.string.correct_toast;
        } else {
            messageId = R.string.incorrect_toast;
        }
        Toast.makeText(this,messageId,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_INDEX,mCurrentIndex);
        outState.putBoolean(BOOLEAN_INDEX, mIsCheater);
        Log.d("What value is there? ->", mIsCheater + " ");

    }

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

And the code from second activity just in case:

package com.example.macbookair.androidbooktutorials;

import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends AppCompatActivity {

    private boolean mAnswerIsTrue;
    private TextView mAnswerTextView;
    private Button mShowAnswer;


    public static final String EXTRA_ANSWER_IS_TRUE = "com.example.macbookair.androidbooktutorials.answer_is_true";
    public static final String EXTRA_ANSWER_SHOWN = "com.example.macbookair.androidbooktutorials.answer_is_showed";
    public static final String INDEX = "cheatIndex";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cheat_activity);

        mAnswerTextView = (TextView) findViewById(R.id.answerTextView);
        if(savedInstanceState != null) {
            String text = savedInstanceState.getString(INDEX,mAnswerTextView.toString());
            mAnswerTextView.setText(text);
        }

        mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

        mShowAnswer = (Button) findViewById(R.id.showAnswerButton);

        setAnswerShownResult(false);

        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);
            }
        });



    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(INDEX, mAnswerTextView.getText().toString());

    }


    private void setAnswerShownResult(boolean isAnswerShown) {
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(RESULT_OK, data);
    }


}

1 Answers1

0

You have this

mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

But when you rotate, I don't think that Intent data is saved unless you explicitly do that yourself.

You also need to save mAnswerIsTrue within onSaveInstanceState and retrieve it within onCreate or onRestoreInstanceState

Also, if I remember correctly, you should put the data before super.onSaveInstanceState(outState);

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245