I have come across some very unexpected (and incredibly frustrating) functionality while trying to restore the state of a list of CheckBox
es after a screen rotation. I figured I first would try to give a textual explanation without the code, in case someone is able to determine a solution without all the gory details. If anyone needs more details I can post the code.
I have a scrolling list of complex View
s that contain CheckBox
es. I have been unsuccessful in restoring the state of these check boxes after a screen rotation. I have implemented onSaveInstanceState
and have successfully transferred the list of selected check boxes to the onCreate
method. This is handled by passing a long[]
of database ids to the Bundle
.
In onCreate()
I check the Bundle
for the array of ids. If the array is there I use it to determine which check boxes to check when the list is being built. I have created a number of test methods and have confirmed that the check boxes are being set correctly, based on the id array. As a last check I am checking the states of all check boxes at the very end of onCreate(
). Everything looks good... unless I rotate the screen.
When I rotate the screen, one of two things happens: 1) If any number of the check boxes are selected, except for the last one, all check boxes are off after a rotation. 2) If the last check box is checked before rotation, then all check boxes are checked after rotation.
Like I said, I check the state of the boxes at the very end of my onCreate()
. The thing is, the state of the boxes at the end of onCreate
is correct based on what I selected before the rotation. However, the state of the boxes on the screen does not reflect this.
In addition, I have implemented each check box's setOnCheckChangedListener()
and I have confirmed that my check boxes' state's are being altered after my onCreate
method returns.
Anyone have an idea of what is going on? Why would the state of my check boxes change after my onCreate
method returns?
Thanks in advance for your help. I have been trying to degub this for a couple days now. After I found that my check boxes were apparently changing somewhere outside my own code I figured it was time to ask around.
I had similar problem. App had several checkboxes on screen.
After rotating phone app was 'manually' setting value for all checkboxes.
This code was executed in onStart().
But on screen all checkboxes were set with value of 'last checkbox' on screen.
Android was calling onRestoreInstanceState(..) and somehow was treating all checkboxes as 'one' [last from screen].
Solution were to disable 'restoring instance state':
<RadioButton
...
android:saveEnabled="false" />