Skip to content Skip to sidebar Skip to footer

How To Get Switch Button Status And Pass To Another Activity Using Shared Preferences

I am having switch button in one activity and another switch button in another activity.In my application i want to get the status of switch button weather it was on on or off stat

Solution 1:

Add a singleton class and create methods for setting and getting state of your toggle button. Later you can access the state of the toggle button from anywhere. You can also use shared preference for this. Set State:

SharedPreferences.Editor editor = getSharedPreferences(
            "com.ali.myapp", Context.MODE_PRIVATE).edit();
editor.putBoolean("state", yourButton1.isChecked());
editor.apply();

Get State:

SharedPreferencesprefs= getSharedPreferences(
            "com.ali.myapp", Context.MODE_PRIVATE); 
boolean state= prefs.getBoolean("state", false);
yourButton2.setChecked(state);

Post a Comment for "How To Get Switch Button Status And Pass To Another Activity Using Shared Preferences"