-1

I'm working on Android sdk platform. Working of app - A user selects multiple checkboxes and then on the basis of selection, the user would receive notifications. When a user relaunches the app, the selected checkboxes stay checked. What should be my approach? The below code is for one checkbox. I want to perform it for multiple checkboxes. How do I approach this?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.arham.csdevbin.downloadimagefrominternet.SharedPreferencesDemo">
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="103dp"
        android:layout_marginStart="103dp"
        android:layout_marginTop="136dp"
        android:text="CheckBox" />
</RelativeLayout>

MainActivity.java

 CompoundButton.OnCheckedChangeListener {
    CheckBox checkBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);
        checkBox = findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener(this);
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            checkBox.setText("The CheckBox is Checked");
        } else {
            checkBox.setText("The CheckBox is not Checked");
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
        SharedPreferences sharedPreferences = getPreferences(0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("CHECK_BOX_VALUE", checkBox.getText().toString());
        editor.putBoolean("CH", checkBox.isChecked());
        editor.commit();
    }
    @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
        SharedPreferences sharedPreferences = getPreferences(0);
        boolean checkBoxValue = sharedPreferences.getBoolean("CH", false);
        String chBoxString = sharedPreferences.getString("CHECK_BOX_VALUE", "This is my CheckBox");
        checkBox.setChecked(checkBoxValue);
        checkBox.setText(chBoxString);
    }
}
lugiorgi
  • 473
  • 1
  • 4
  • 12
  • 1
    Try to save value inside SharedPreference inside `onCheckedChanged` method instaed of using it in `onPause` – S.Ambika Jan 30 '20 at 06:48
  • The above code has worked for one checkbox. I want it for multiple checkboxes. – Cricket_Fan Jan 30 '20 at 06:49
  • Have you tried adding more checkboxes? – Ryan M Jan 30 '20 at 06:50
  • The above code will _not_ work if the user directly swipes out the app of recent apps. @S.Ambika is right - save the preference of each checkbox directly in the ``onCheckedChanged`` when the checkbox changes. works always. – Grisgram Jan 30 '20 at 06:51
  • Multiple Checkboxes as individual or inside recycler View? – S.Ambika Jan 30 '20 at 06:51
  • I tried to do it for multiple checkboxes. Result - Successive checkboxes retaining value of first checkbox. T – Cricket_Fan Jan 30 '20 at 06:52
  • May be something like this can help you – S.Ambika Jan 30 '20 at 07:01
  • I tried other methods too. It's not working at all. I'm new to android programming. Wish I could post screenshot of my app. Since Android has concept of sharedprefernces, why should we choose a complicated approach? – Cricket_Fan Jan 30 '20 at 07:05
  • are you using multiple checkbox inside recyclerview items or just use in normal view? – Avinash Jan 30 '20 at 07:25
  • multiple checkbox - normal view – Cricket_Fan Jan 30 '20 at 07:29
  • _"What should be my approach?"_ Follow [the Android documentation](https://developer.android.com/training/data-storage). And SharedPreferences supports collections of simple data with the `Editor.putStringSet()` and `getStringSet()` methods. You just need to save the CheckBox states as strings. Could be "0" and "1" or "false" and "true". Whatever works conveniently. – Markus Kauppinen Jan 30 '20 at 08:48
  • Well show complete code for three ceckboxes for example. All this talking... we dont know what you tried. – blackapps Jan 30 '20 at 08:48
  • If you have checkBox1, checkBox2 and checkBox3 then what is the problem of saving three states to shared preferences? But first try to let it work for one. – blackapps Jan 30 '20 at 08:50
  • I tried above code for two checkboxes. – Cricket_Fan Jan 30 '20 at 08:59
  • 1stCheckbox - Animal Sciences, 2nd checkbox - Fisheries. Result - 2nd checkbox retains the value of 1st checkbox – Cricket_Fan Jan 30 '20 at 09:00
  • Instead of Fisheries, it shows Animal Sciences. Now there are duplicate values of Animal Sciences – Cricket_Fan Jan 30 '20 at 09:02

1 Answers1

1

You have used SharedPreference and saved only one checkbox value. If you want to save multiple checkbox value you must store all selected checkbox values on SharedPreference.
I suggest you to save multiple values, use HashMap to get and set checkboxes in SharedPreference.

SharedPreferences preferences;
public SharedPreferences.Editor editor;

public void setCheckboxes(HashMap<String, boolean> checkboxes) {

    editor.putString("checkbox_1", checkboxes.get("checkbox_1"));
    editor.putString("checkbox_2", checkboxes.get("checkbox_2"));
    editor.putString("checkbox_3", checkboxes.get("checkbox_3"));
    editor.putString("checkbox_4", checkboxes.get("checkbox_4"));
    editor.apply();
}

public HashMap<String, boolean> getCheckboxes() {
    HashMap<String, boolean> checkboxes = new HashMap<String, boolean>();
    checkboxes.put("checkbox_1", preferences.getString("checkbox_1", false));
    checkboxes.put("checkbox_2", preferences.getString("checkbox_2", false));
    checkboxes.put("checkbox_3", preferences.getString("checkbox_3", false));
    checkboxes.put("checkbox_4", preferences.getString("checkbox_4", false));

return checkboxes;

}
Mashood Murtaza
  • 477
  • 5
  • 14