Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving `RadioButton` state on Orientation change

Tags:

android

  1. My app has a Fragment attached to an Activity
    (which would ask yes or no questions to users.)

  2. I have two RadioButton for each question
    (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).

How to solve this? I ended here only after extensive google-ing. Please help.

like image 365
keerthana murugesan Avatar asked Dec 03 '25 03:12

keerthana murugesan


1 Answers

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(); 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.

Reading http://developer.android.com/training/basics/activity-lifecycle/index.html should provide more information.

like image 64
hoss Avatar answered Dec 05 '25 18:12

hoss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!