Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

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

Open
Testator opened this issue Jun 21, 2019 · 4 comments
Open

Comments

@Testator
Copy link

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!

@egesamichael
Copy link

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

@32attiya
Copy link

32attiya commented May 11, 2020

@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

@egesamichael
Copy link

@32attiya Thanks let me try it.

@wissedenis
Copy link

wissedenis commented Jun 16, 2021

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

            }

        });


}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants