I am having 5 radiobuttons in my application and i want to save their states so that when i exit and then come back to the application then i see the same button clicked which was clicked prior to my exiting the application
Following is my code for saving the state
private int flag1 = true;
private void save(final boolean isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check", isChecked);
editor.commit();
}
private boolean load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
return sharedPreferences.getBoolean("check", true);
}
protected void onPause() {
super.onPause();
save(!flag1);
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
save(load());
Toast.makeText(getApplicationContext(),""+load(), Toast.LENGTH_LONG).show();
}
I am calling the save method from inside the onCheckedChangeListener for every radiobutton but nothing happens.
Is there any other method of using sharedpreferences ??
See Question&Answers more detail:os