简体   繁体   English

在方向改变时保存`RadioButton`状态

[英]Saving `RadioButton` state on Orientation change

  1. My app has a Fragment attached to an Activity 我的应用程序附加了一个Activity Fragment
    (which would ask yes or no questions to users.) (这会向用户提出是或否问题。)

  2. I have two RadioButton for each question 我为每个问题都有两个RadioButton
    (one for 'yes' and one for 'No'.) (一个用于'是',一个用于'否'。)

  3. Based on user's choice for each question the next question will be loaded. 根据用户对每个问题的选择,将加载下一个问题。 For example, 例如,

    1. Are you at the accident scene?
    [ yes ] [ No ]

    • If the user answers 'yes' another question will be loaded. 如果用户回答“是”,则将加载另一个问题。
    • If 'no' some other question will be loaded. 如果'不',则会加载其他一些问题。

Now my problem is, when I change the orientation after the visibility of second question, only the first question appear(with RadioButton checked). 现在我的问题是,当我在第二个问题的可见性之后改变方向时,只出现第一个问题(选中RadioButton )。

How to solve this? 怎么解决这个? I ended here only after extensive google-ing. 我只是在广泛的谷歌之后才结束这里。 Please help. 请帮忙。

Without seeing a little more code it's hard to tell what the exact issue is but heres a good way to think of your issue: 如果没有看到更多的代码,很难说出确切的问题是什么,但这是一个思考问题的好方法:

When you rotate the device the activity saves its state and then restarts in the new orientation. 旋转设备时,活动会保存其状态,然后以新方向重新启动。 super.onSaveInstanceState(); and super.onRestoreInstanceState(); super.onRestoreInstanceState(); take care of saving view states but they donot know about any other variables (like ones keeping track of what question the user was on). 注意保存视图状态,但他们不知道任何其他变量(如跟踪用户所处的问题的变量)。

Override onSave and onRestore to save / load your data during this transition. 在此转换期间,覆盖onSave和onRestore以保存/加载数据。

Reading http://developer.android.com/training/basics/activity-lifecycle/index.html should provide more information. 阅读http://developer.android.com/training/basics/activity-lifecycle/index.html应该提供更多信息。

Hi please add like this in your manifest 嗨,请在清单中添加这样的内容

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

your activity wont refresh if the user changes the orientation . 如果用户更改方向,您的活动将不会刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM