2

I have created an android app it works perfectly fine,but when i change the position of my phone to landscape mode all the all data gets reset that is all the text on the buttons of the game gets replaced by blank values.

I am trying to use onSaveInstanceState(Bundle outState) method to solve it

I have to save a 3 dimensional Character array(i.e. i have declare it by char[][][] a=newchar[3][3][3])

I am using the following code to save it

    public void onSaveInstanceState(Bundle outState)
{
    outState.putCharArray(char[][][] "a", a);
    super.onSaveInstanceState(outState);
}

but it gives the following error

Multiple markers at this line
- Syntax error on token(s), misplaced construct(s)
- The method putCharArray(String, char[]) in the type Bundle is not applicable for the arguments (String, 
 char[][][])

2 Answers 2

3

Arrays are serializable. You could try:

public void onSaveInstanceState(Bundle outState){
    char[][][] a=new char[3][3][3];
    outState.putSerializable("a", a);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
     char[][][] a = (char[][][]) savedInstanceState.getSerializable("a");

     if(a != null)
     {
          //Do something with a
     }

}
Sign up to request clarification or add additional context in comments.

2 Comments

when i use the idea of your code it give the following error """Cannot make a static reference to the non-static method getSerializable(String) from the type Bundle"""
I've updated my answer. Be sure your using an actual instance of a Bundle instead of the Bundle class itself.
2

This worked storing an array in the onSaveInstanceState(Bundle savedInstanceState) method:

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{ 
    for (int count = 0; count < gameMovesArray.length; count++)
    {
    String gameMovesArrayCountString = "gamesMovesArray" + Integer.toString(count); //concatenated string
    savedInstanceState.putInt(gameMovesArrayCountString, gameMovesArray[count];
    } 
    //...
super.onSaveInstanceState(savedInstanceState);
}

Then to repopulate the array in the onRestoreInstanceState(Bundle savedInstanceState) method:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) 
{
    super.onRestoreInstanceState(savedInstanceState);
    for(int count = 0; count < gameMovesArray.length; count++)
    {
    String gameMovesArrayCountString = "gamesMovesArray" + Integer.toString(count);
    gameMovesArray[count] = savedInstanceState.getInt(gameMovesArrayCountString);
    }
//...
}

Comments

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.