0

I am trying to write a settings activity in an application on Eclipse. In the Main Activity, it has a button that runs a certain command. In the settings activity, I want to have a checkbox that when checked, changes what the button in the Main Activity runs when it is tapped. Right now, I have it so that when the checkbox is checked, it changes the value of a boolean and passes it to the main activity. When the button in the main activity is tapped, it checks to see if the boolean is true or false. All of this works perfectly, but when I return to the settings activity after that, the checkbox is unchecked. What should I do to have it stay checked after I go to another activity?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • You need to save the state of the activity http://stackoverflow.com/questions/151777/saving-activity-state-in-android – chancea Jul 10 '13 at 17:32

2 Answers2

1

I believe the comment I posted is the answer:

You need to save the state of the activity. This information can be found at Saving Android Activity state using Save Instance State but in short you need to override these two methods:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
}

and

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);

}
Community
  • 1
  • 1
chancea
  • 5,858
  • 3
  • 29
  • 39
1

you can use shared preference in android to store state. take look at this

http://developer.android.com/guide/topics/data/data-storage.html#pref

and

http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

Shiva
  • 754
  • 6
  • 16
  • This shows how to save the boolean but how do I have the checkbox be checked when I return to the activity? – JoshSchellenberger Jul 11 '13 at 16:36
  • you can check status for that check box in shared preference in (on create method of activity) and set the check box according to it,and again when user changed checked status put that updated value in shared pref. – Shiva Jul 12 '13 at 06:53