0

I am very new to Java/Android Development/programming. Recently I am working on tutorials from book, and trying to solve problems outside the scope of text. I ran into problems.

On orientation change, I wish to saved an array of string and boolean using onSaveInstanceState, and.. well, I don't know how. The putArray seems to be only available for one type of data (putIntArray, putBooleanArray, etc), and I'm at lost as to how I can solve this problem.

The array I'm trying to save:

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

Is there a simple option? What are my choices?

2 Answers 2

0

First, make sure your TrueFalse class implements Serializable.

Then you can put the array onSaveInstanceState(), and retrive it onRestoreInstanceState()

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putSerializable("my_array", mQuestionBank);
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    mQuestionBank = savedInstanceState.getSerializable("my_array");
    super.onRestoreInstanceState(savedInstanceState);
}
0
0

Your TrueFalse class needs to implement the Parcelable interface. Once you've done that, I believe you can use savedInstanceState.putExtra(String key,Parcelable[] yourArray)

You can also implement Serializable but, from what I've heard, there are some performance issues.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.