Android CheckBox -- 屏幕旋转后恢复状态 [英] Android CheckBox -- Restoring State After Screen Rotation

查看:22
本文介绍了Android CheckBox -- 屏幕旋转后恢复状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在屏幕旋转后尝试恢复 CheckBox 列表的状态时,我遇到了一些非常意外(并且非常令人沮丧)的功能.我想我首先会尝试在没有代码的情况下给出文本解释,以防有人能够在没有所有血腥细节的情况下确定解决方案.如果有人需要更多详细信息,我可以发布代码.

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.

我有一个包含 CheckBox 的复杂 View 的滚动列表.在屏幕旋转后,我未能成功恢复这些复选框的状态.我已经实现了 onSaveInstanceState 并成功地将选中的复选框列表转移到了 onCreate 方法.这是通过将数据库 ID 的 long[] 传递给 Bundle 来处理的.

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 transferred the list of selected check boxes to the onCreate method. This is handled by passing a long[] of database ids to the Bundle.

onCreate() 中,我检查了 Bundle 中的 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?

预先感谢您的帮助.几天来我一直试图对此进行调试.在我发现我的复选框显然在我自己的代码之外的某个地方发生了变化后,我想是时候四处打听了.

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.

推荐答案

我遇到了类似的问题.应用程序在屏幕上有几个复选框.
旋转手机应用程序后手动"设置所有复选框的值.
此代码在 onStart() 中执行.
但在屏幕上,所有复选框都设置为屏幕上的最后一个复选框".
Android 正在调用 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" />

这篇关于Android CheckBox -- 屏幕旋转后恢复状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆