swipe-button icon indicating copy to clipboard operation
swipe-button copied to clipboard

how do i keep the toggle state active when switching between fragments

Open Testator opened this issue 5 years ago • 4 comments

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!

Testator avatar Jun 21 '19 08:06 Testator

Hello @Testator? were you able to find a solution for this?

egesamichael avatar Apr 17 '20 22:04 egesamichael

@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 avatar May 11 '20 11:05 32attiya

@32attiya Thanks let me try it.

egesamichael avatar May 12 '20 18:05 egesamichael

@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);
                }

            }

        });


}

wissedenis avatar Jun 16 '21 06:06 wissedenis