Reputation:
Can anyone tell me why this doesn't work please? I am trying to save the state of the togglebutton, but everytime I close the app and start again, it goes back to off. I want the state to be remembered, so when someone adds something to their favourites, to remember it as being added to favourite.
private String state = "State";
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.recipe_menu, menu);
toggleButton = (ToggleButton) findViewById(R.id.myToggleButton);
toggleButton.setChecked(readState());
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
// The toggle is enabled
toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_favorite_black_24dp));
else
// The toggle is disabled
toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_favorite_border_black_24dp));
saveState(isChecked);
}
});
return super.onCreateOptionsMenu(menu);
}
private void saveState(boolean isFavourite) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
sharedPreferences.edit().putBoolean(state, isFavourite).apply();
}
private boolean readState() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
return sharedPreferences.getBoolean(state, true);
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
toggleButton = (ToggleButton) findViewById(R.id.myToggleButton);
toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_favorite_border_black_24dp));
toggleButton.setChecked(readState());
}
Upvotes: 0
Views: 846
Reputation: 28179
you're reading from one SharedPreferences
file, but writing into a different one.
change the init line of sharedPreferences
in saveState
to:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Also, instead of using setOnClickListener
, you should instead use setOnCheckedChangeListener
, see the docs, and the example code:
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});
UPDATE
Full code with fixes:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.recipe_menu, menu);
toggleButton = (ToggleButton) findViewById(R.id.myToggleButton);
toggleButton.setChecked(readState());
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_favorite_black_24dp));
} else {
// The toggle is disabled
toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_favorite_border_black_24dp));
}
saveState(isChecked);
}
});
return super.onCreateOptionsMenu(menu);
}
private void saveState(boolean isFavourite) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
sharedPreferences.edit().putBoolean("State", isFavourite).apply();
}
private boolean readState() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
return sharedPreferences.getBoolean("State", true);
}
Upvotes: 1
Reputation: 1
toggleButton.setChecked(sharedPreferences.getBoolean("toggleButton", true));
The above line is resetting the state of checkbox. Hence it needs to be removed. Moreover the Strings like "Favourite" and "State" should be defined as constants.In case of spelling mistakes in one or the other line,this can also lead to unexpected results.
Upvotes: 0
Reputation: 1827
You are changing the state of your togglebutton twice. So, these lines are not necessary. Delete the below lines.
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
toggleButton.setChecked(sharedPreferences.getBoolean("toggleButton", true));
Hope this helps..
Upvotes: 0