1

Hi I've an activity that shows a toggle button. The issue is that, when I press the back button on the device and then I come back to my activity, the state of the button is reset.

How can I do to make the activity "remember" the state of the button through its entire life cycle?

user1071138
  • 656
  • 3
  • 12
  • 30

3 Answers3

2

Do this when in onCreate() or onResume() as per your requirement :

SharedPreferences preferences = this.getSharedPreferences(Constants.SENDEVENT_PREFERENCE,Context.MODE_PRIVATE);
if(preferences.getBoolean(Constants.SENDEVENT_PREFERENCE_SAVE_LIBRARY, false))
            lib.setChecked(true);

And in onCheckedChanged method do this :

if (buttonView == lib) {
            if (isChecked) {
                prefsEditor.putBoolean(Constants.SENDEVENT_PREFERENCE_SAVE_LIBRARY,
                        true);
                prefsEditor.commit();
            } else {
                prefsEditor.putBoolean(Constants.SENDEVENT_PREFERENCE_SAVE_LIBRARY,
                        false);
                prefsEditor.commit();
            }
}
baldguy
  • 2,090
  • 1
  • 16
  • 25
1

You'll have to save the state in a persistent memory. The Shared Preferences is your best option in this case I'd say.

Something like (inside your activity with the toggle button):

public void saveButtonState(boolean pressed) {
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("TOGGLE_BUTTON_STATE", pressed);
    editor.commit();
}

Then in onCreate you can set the buttons state by reading "TOGGLE_BUTTON_STATE" from SharedPreferences.

jelgh
  • 705
  • 1
  • 6
  • 22
1

The reason, why the state is reseted is that your activity is completely destroyed and then restored again due to Android architecture. To store state of your button you should rather save it state into the Bundle in onSaveInstaceState() and restore in onRestoreInstanceState() - these methods are designed specifically for this kind of tasks.

So the answer is you should implement saving/restoring button state in two methods:

@Override
protected void onSaveInstanceState (Bundle outState) {
    // Your code here
}

@Override
protected void onRestoreInstanceState (Bundle savedInstanceState) {
    // Your code here
}

Details see in docs:

Note the "Killable" column in the above table -- for those methods that are marked as being killable, after that method returns the process hosting the activity may killed by the system at any time without another line of its code being executed. Because of this, you should use the onPause() method to write any persistent data (such as user edits) to storage. In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created. See the Process Lifecycle section for more information on how the lifecycle of a process is tied to the activities it is hosting. Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.

Prizoff
  • 4,486
  • 4
  • 41
  • 69