0

android: data gets lost when phone changed to landscape mode of TIC-TAC-TOE game

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.

Since i am new to android programming i am unable to sort this problem, please help. thank you.

I'm not posting the XML code and java code cause its very big, if you want to see the XML code or java code,then please comment.

Anuj
  • 994
  • 11
  • 21

3 Answers3

0

Regarding that you might have already handled the savedInstanceState, then include

android:configChanges="orientation"

in the activity in android manifest.

luisfer
  • 603
  • 2
  • 8
  • 15
0

May be it case for activity reload

there is a way you can try:Simply add the "screenSize" attribute like I did below:

<activity
    android:name=".YourActivityName"
    android:configChanges="orientation|screenSize">
</activity>

more details: about it http://developer.android.com/guide/topics/manifest/activity-element.html

and

http://developer.android.com/guide/topics/resources/runtime-changes.html

Saifuddin Sarker
  • 853
  • 3
  • 8
  • 26
0

You need to override the onSaveInstanceState method. When that method is called, save the data you want to keep to the bundle object. Then in onCreate you should be checking if bundle != null), and if that is true (meaning you saved stuff in the onSaveInstanceState method, update your variables based on values in the bundle.

From the Android documentation, you should avoid using the android:configChanges property:

Note: Using this attribute should be avoided and used only as a last resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.

Ben Kane
  • 9,331
  • 6
  • 36
  • 58
  • im new to android programming, so if you can elaborate your answer more then it would be helpful – Anuj Apr 02 '14 at 15:20
  • Please see this Stack Overflow answer for an example of how to use `onSaveInstanceState` properly: http://stackoverflow.com/a/6525808/2448305 – Ben Kane Apr 02 '14 at 15:22
  • thnx i will check it implement and if it works i will notify you – Anuj Apr 02 '14 at 15:25
  • how to save 'int's or 'char's for example i have saved my data into an variable 'a' which is an it so how to code to save it using your idea of putInt please tell the complete code of saving that variable 'a' – Anuj Apr 02 '14 at 15:39
  • Remember to accept the answer if this worked for you :) – Ben Kane Apr 02 '14 at 15:45
  • i have a 3 dimensional array a(i.e. i have declared it "char[][][] a=new char[3][3][3];" this way now i want to save it how do i do that "outState.putCharArray("a", a);" in this way? ) – Anuj Apr 02 '14 at 15:50