Android的复选框 - 还原状态屏幕旋转后复选框、屏幕、状态、Android

2023-09-05 06:46:59 作者:地球老子捏圆滴

我所遇到的一些非常意外(和非常令人沮丧)的功能,同时尝试了屏幕旋转后恢复复选框列表的状态。我想我首先会尝试给一个文字说明没有code,万一有人能够确定一个解决方案,而所有的血淋淋的细节。如果有人需要更多的细节我可以张贴code。

I have come across some very unexpected (and incredibly frustrating) functionality while trying to restore the state of a list of CheckBoxes 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.

我有一个包含复选框的复杂视图的滚动列表。我已经不成功恢复这些复选框的状态的屏幕旋转之后。我已经实现的onSaveInstanceState,并已成功地转移了选中的复选框的列表中onCreate方法。这是通过处理长[]数据库ID为捆绑的。

I have a scrolling list of complex Views that contain CheckBoxes. I have been unsuccessful in restoring the state of these check boxes after a screen rotation. I have implemented onSaveInstanceState and have successfully transfered the list of selected check boxes to the onCreate method. This is handled by passing a long[] of database ids to the Bundle.

在的onCreate(),我检查包的ID数组。如果数组有没有我用它来确定哪些复选框检查时,列表正在修建。我创建了一些测试方法和已确认的复选框被设置正确,基于ID阵列。作为最后的检查,我检查所有复选框的状态时的onCreate的最后()。一切看起来都不错......除非我旋转屏幕。

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.

当我旋转屏幕,两种情况之一发生:1)如果任何数量的复选框被选中,除了最后一个,所有复选框都掀起了旋转后。 2)如果最后一个复选框旋转前检查,然后所有复选框的检查的后旋。

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.

就像我说的,我检查的框的状态在我的onCreate()的最后。问题是,这些箱子在的onCreate结束后状态的正确的依据是什么我转动之前选择。然而,在屏幕上的框的状态不会反映这一点。

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.

另外,我已经实现了每个复选框的setOnCheckChangedListener()和我已经证实,我的复选框国家正在修改的在的我的onCreate方法返回。

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.

人有是怎么回事的想法?为什么我的onCreate方法返回后,我的复选框状态更改?

Anyone have an idea of what is going on? Why would the state of my check boxes change after my onCreate method returns?

在此先感谢您的帮助。我一直在努力,现在到degub这几天。之后,我发现我的复选框显然超出了我自己的code某处改变我想是时​​候问问身边。

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.

推荐答案

我有类似的问题。应用对屏幕上几个复选框。 经过旋转手机的应用程序是手动设定值的所有复选框。 这code是在ONSTART()执行。 但是,在屏幕上的所有复选框被设置在屏幕上最后一个复选框的价值。 机器人在呼唤onRestoreInstanceState(..),并以某种方式被对待所有的复选框为一[最后从屏幕上。

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" />