kotlinstateandroid-preferences

Keep checkbox state after shutting down app


i have app with multiple checkboxes, which gets disabled after checking all three of them. The problem is, when i close the app, the state doesnt presist. My code:

private fun setCheckedChangedListener() {
        listOf(binding.checkTaskOne,binding.checkTaskTwo,binding.checkTaskThree).forEach {
            it.setOnCheckedChangeListener{ _ , _ ->
                if(binding.checkTaskOne.isChecked && binding.checkTaskTwo.isChecked && binding.checkTaskThree.isChecked) {
                    binding.checkTaskOne.isEnabled = false
                    binding.checkTaskTwo.isEnabled = false
                    binding.checkTaskThree.isEnabled = false
                    binding.textView2.setText(R.string.splneno)

                }
            }
        }
    }

I tried experimenting with the onDestroy function and Prefrences, but i dont exactly know how it works.

override fun onDestroy() {
        super.onDestroy()
        save(binding.checkTaskOne.isChecked())
        save(binding.checkTaskTwo.isChecked())
    }

    override fun onResume() {
        super.onResume()

    }

    private fun save(boolean: Boolean) {
        val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
        with (sharedPref.edit()) {
            putBoolean("check", true)
            apply()
        }
    }

Solution

  • There are few things to do. Most important thing is, you do not load saved preferences at all. Also, you save preference every time at the same name - it won't work, every setting must have separate name to let runtime distinguish between them.

    I have modified method which does saving and add one which does loading. I call it in onCreate() method, which is called on the beginning.

    By the way, there are still some things to improve, like avoid repetition of getting Shared Preferences handler and moving name of every preference for example into constants section, to avoid making mistake during typying them in load and save methods usages.

    Code:

    override fun onCreate(savedInstanceState: Bundle?) {
            [...] //things like initialize binding, etc.
    
            binding.checkTaskOne.isChecked = load("checkTaskOne")
            binding.checkTaskTwo.isChecked = load("checkTaskTwo")
            binding.checkTaskThree.isChecked = load("checkTaskThree")
    
        }
    
    override fun onDestroy() {
            super.onDestroy()
            save("checkTaskOne", binding.checkTaskOne.isChecked())
            save("checkTaskTwo", binding.checkTaskTwo.isChecked())
            save("checkTaskThree", binding.checkTaskThree.isChecked())
        }
    
    private fun load(name: String): Boolean {
            val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return false
            return sharedPref.getBoolean(name, false)
        }
    
        private fun save(name: String, boolean: Boolean) {
            val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
            with(sharedPref.edit()) {
                putBoolean(name, boolean)
                apply()
            }
        }