Use Sharedpreferences to save / get the status of ToggleButton
(checked / unchecked)
When starting Activity, check the saved value in the preferences, and programmatically change the status of the agreed button.
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); // Contexto
ToggleButton toggleButton = findViewById(R.id.toggleButton);
toggleButton.setChecked(sp.getBoolean("meuToggleButton", false));
...
To save the value in the preferences, use the Oncheckedchangelistener, thus, whenever the button is clicked its value will be saved (and later read upon starting Activity)
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("meuToggleButton", isChecked);
editor.apply();
}
});
What code you use to save Edittext?
– ramaral
I used Sharedpreferences. I implemented the example below and it worked perfectly.
– Thed Santana