I am new to Android development and am having a question about multiple switch buttons. How to retrieve values smartly and make this data available for other activities?
To save the button state and retrieve your information in this activity I do it as follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
switchButtonOne = findViewById(R.id.switchButtonOne);
switchButtonOne.setChecked(true);
switchButtonTwo = findViewById(R.id.switchButtonTwo);
switchButtonTwo.setChecked(false);
...... (In total there are 5 switch buttons)
//switchButtonOne
switchButtonOne.setOnCheckedChangeListener((compoundButton, isChecked) -> {
if(isChecked){
stringArrayList.add("1");
}else {
stringArrayList.remove("1");
}
getArray(stringArrayList);
//SAVE
sharedPreferences[0] = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences[0].edit();
editor.putBoolean("1", switchButtonOne.isChecked());
editor.commit();
});
switchButtonOne.setChecked(sharedPreferences[0].getBoolean("1", false));
//switchButtonTwo
switchButtonTwo.setOnCheckedChangeListener((compoundButton, isChecked) -> {
if(isChecked){
stringArrayList.add("2");
}else {
stringArrayList.remove("2");
}
getArray(stringArrayList);
//SAVE
sharedPreferences[0] = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences[0].edit();
editor.putBoolean("2", switchButtonTwo.isChecked());
editor.commit();
});
.......
As you can see I do not have much knowledge in Android development! That's why I'm very lost! Can you give me a hand? On leaving part available for other activities I could not even get out of the way.
Thank You
Hari