Issue
Functionality: I have three activities in my application, in activity one i have 2 set of radio button. When user click yes on first set of radio button he moves to activity 2nd. When he come back to original activity the radio button should be checked. After that if he click the second yes he should be taken to activity 3rd. and similarly both the radio button should be checked yes when he comes back to main activity.
Problem: i am able to perform almost every part of functionality except when he comes back from 3rd activity only one radio button is selected.
Solution
Another approach you could use is to save the state of the radio button in the SharedPreferences. And when you come back to your main activity, you can recover it from the same place with the same key. This approach allows you to set the radio buttons even if the user closes the activity.
Saving the key:
SharedPreferences sharedPref = MainActivity.this.getSharedPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("state_of_1st_radio_button",true);
// true or false depending on what you want to save.
editor.commit();
Getting the key:
SharedPreferences sharedPref = MainActivity.this.getSharedPreferences(Context.MODE_PRIVATE);
if(sharedPref.getBoolean("state_of_1st_radio_button",false) == true){
//set the radio button true
}
Note that the second argument in sharedPref.getBoolean method is DEFAULT VALUE, meaning that if there is no object to retrieve form SharedPrefs, it will return that default value.
Hope this is helpful.
Answered By - Goktug Basaran
Answer Checked By - David Marino (JavaFixing Volunteer)