swipe-button
swipe-button copied to clipboard
how do i keep the toggle state active when switching between fragments
Hi thanks for the great feature...
I am trying to save the state of the button when switching between fragments. I tried sharedpreferences but it did not work.
everytime i navigate back to the fragment from another, it is in the off state. How do I keep it in the on state until i turn it off.
thanks in advance!
Hello @Testator? were you able to find a solution for this?
@Testator , @egesamicheal i have fixed that , i made static global boolean variable as swipebutton switch and when i swipe it i save it state using this variable in shared prefrences and that's it
@32attiya Thanks let me try it.
@32attiya please I've struggled to achieve this could you please give me a sample code how u did, this is how i did it but it's not working for me
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
swipeButton = findViewById(R.id.swipe_button);
sp = getSharedPreferences("save", Context.MODE_PRIVATE);
Boolean statusLocked = sp.edit().putBoolean("locked", true).commit();
// swipeButton.setActivated(sharedPreferences.getBoolean("value", true));
swipeButton.setOnStateChangeListener(new OnStateChangeListener() {
@Override
public void onStateChange(boolean active) {
if(active){
SharedPreferences.Editor editor = getSharedPreferences("save", MODE_PRIVATE).edit();
editor.putBoolean("value", true);
editor.apply();
swipeButton.setActivated(true);
Toast.makeText(MapsActivity.this,
"Active", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MapsActivity.this, FullscreenActivity.class);
startActivity(intent);
}else {
SharedPreferences.Editor editor = getSharedPreferences("save", MODE_PRIVATE).edit();
editor.putBoolean("value", false);
editor.apply();
swipeButton.setActivated(false);
Toast.makeText(MapsActivity.this,
"Not active", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MapsActivity.this, FullscreenActivity.class);
startActivity(intent);
}
}
});
}