Use SharedPreferences to save / get the status of ToggleButton
(checked / unchecked)
When you start the activity, check the preferences in the preferences, and programmatically change the status of the button accordingly.
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); // Contexto
ToggleButton toggleButton = findViewById(R.id.toggleButton);
toggleButton.setChecked(sp.getBoolean("meuToggleButton", false));
...
To save the value in preferences, use the OnCheckedChangeListener , so always that the button is clicked its value will be saved (and later read when starting the 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();
}
});