I have a problem in saving state of check state of radio button and checkbox. How can I save check-state of these elements by using onSaveInstanceState and onRestoreInstanceState?
It is a simple quiz app. AFter finish button is clicked, a new score sheet is opened and correct answers turn to green.but at the same time, when orientation is changed, user's selections are lost.
Here is the code:
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
answer2.setTextColor(0xAA76FF03);
answer3.setTextColor(0xAA76FF03);
answer5.setTextColor(0xAA76FF03);
answer42.setTextColor(0xAA76FF03);
answer43.setTextColor(0xAA76FF03);
answer44.setTextColor(0xAA76FF03);
answer45.setTextColor(0xAA76FF03);
}
protected void onRestoreInstanceState(Bundle savedState)
{
answer2.setTextColor(0xAA76FF03);
answer3.setTextColor(0xAA76FF03);
answer5.setTextColor(0xAA76FF03);
answer42.setTextColor(0xAA76FF03);
answer43.setTextColor(0xAA76FF03);
answer44.setTextColor(0xAA76FF03);
answer45.setTextColor(0xAA76FF03);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.html_new_page);
finishButton = (Button) findViewById(R.id.finish_button);
enterName = (EditText) findViewById(R.id.enter_name);
answer1 = (EditText) findViewById(R.id.answer_1);
answer2 = (RadioButton)findViewById(R.id.answer2) ;
answer3 = (RadioButton)findViewById(R.id.answer3);
answer5 = (RadioButton)findViewById(R.id.answer5);
answer41 = (CheckBox)findViewById(R.id.answer41);
answer42 = (CheckBox) findViewById(R.id.answer42);
answer43 = (CheckBox) findViewById(R.id.answer43);
answer44 = (CheckBox) findViewById(R.id.answer44);
answer45 = (CheckBox) findViewById(R.id.answer45);
finishButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String checkAnswer1 = answer1.getText().toString();
c2 = answer2.isChecked();
c3 = answer3.isChecked();
checkAnswer41 = answer41.isChecked();
checkAnswer42 = answer42.isChecked();
checkAnswer43 = answer43.isChecked();
checkAnswer44 = answer44.isChecked();
checkAnswer45 = answer45.isChecked();
c5 = answer5.isChecked();
if ( checkAnswer1.equals("Hypertext Markup Language") || checkAnswer1.equals("HyperText Markup Language")) {
c1 = true;
answer1.setTextColor(0xAA76FF03);
}
else {
c1 = false;
}
if (checkAnswer41 == false && checkAnswer42 == true && checkAnswer43 == true && checkAnswer44 == true && checkAnswer45 == true){
c4 = true;
}
else {
c4 = false;
}
int q1 = (c1) ? 1 : 0;
int q2 = (c2) ? 1 : 0;
int q3 = (c3) ? 1 : 0;
int q4 = (c4) ? 1 : 0;
int q5 = (c5) ? 1 : 0;
int correctNumber = q1 + q2 + q3 + q4 + q5;
correct = Integer.toString(correctNumber);
Intent i = new Intent();
i.setClass(Activity1.this, ResultActivity.class);
i.putExtra("outData", enterName.getText().toString());
i.putExtra("out", correct);
startActivity(i);
}
});
};
}
Use something like this: (adjust to your own code)
public class YourActivity extends AppCompatActivity {
private static final String CHECKBOX_CHECKED_KEY = "is_checkbox_checked";
private CheckBox myCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState != null) {
myCheckBox.setChecked(savedInstanceState.getBoolean(CHECKBOX_CHECKED_KEY));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState)
outState.putBoolean(CHECKBOX_CHECKED_KEY, myCheckBox.isChecked());
}
}
You will need to define different keys for each CheckBoxe's state you want to save.