I have this challenge whereby i need to track the attendance of the users, So I decided to create swipe-button com.ebanx:swipe-button
where by the user will slide to clock in/out. Well my challenge I can't save it's state. I'd like If a user closes or changes to another activity the change/state is saved. I used Shared Preferences to try to save it but no luck.
I tried to follow this stacks but I didn't find them helpful
How to change state of swipe button from code?
Save Toggle Switch / Checkbox State with SharedPreferences
how do i keep the toggle state active when switching between fragments
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
swipeButton = findViewById(R.id.swipe_button);
SharedPreferences tog_prefs = getSharedPreferences("save", MODE_PRIVATE);
boolean pref_1 = tog_prefs.getBoolean("pref_1", false);
if (pref_1) {
swipeButton.setActivated(true);
} else {
swipeButton.setActivated(false);
}
setContentView(R.layout.activity_attendance);
swipeButton.setOnStateChangeListener(new OnStateChangeListener() {
@Override
public void onStateChange(boolean active) {
if(active){
SharedPreferences.Editor editor = getSharedPreferences("save", MODE_PRIVATE).edit();
editor.putBoolean("pref_1", true); // value to store
editor.commit();
Toast.makeText(Attendance.this,
"Successfully ", Toast.LENGTH_LONG).show();
}else{
SharedPreferences.Editor editor = getSharedPreferences("save", MODE_PRIVATE).edit();
editor.putBoolean("pref_1", false); // value to store
editor.commit();
Toast.makeText(Attendance.this,
"Not active", Toast.LENGTH_LONG).show();
}
}
});