Home > other >  Saving CheckBox States When Switching Between Activities (Kotlin)
Saving CheckBox States When Switching Between Activities (Kotlin)

Time:06-21

I'm making an Android app in Android Studio for my girlfriend to help her keep track of her tasks at work. The app has multiple to-do lists using RecyclerView. Since each checklist has their own activity, and she will be switching between them, how do I save which boxes have been checked so that they remained checked when switching between activities? Here is the RecyclerView adapter code for one of the to-do lists that also contains the Checkbox code.

package com.mattkalichman.coffee

import android.util.SparseBooleanArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.checkbox_row.view.*

class ThreeHourPullAdapter(var list: ArrayList<Data>) :
    RecyclerView.Adapter<ThreeHourPullAdapter.ViewHolder>() {

    private var titles =
        arrayOf(
            "Log into the handheld under Pull to Thaw.",
            "Count anything left on pastry cart (BOH).",
            "Count everything on front pastry cart (FOH).",
            "Put remaining pastries from BOH on FOH cart. Remember to FIFO!",
            "Pull from freezer to BOH cart. Adjust number of pulled items according to inventory.",
            "When pull is done, press complete pull at the bottom of the screen.",
            "Date all pastries with sticker gun by standards. (Marshmallow Dream Bars, Madelines, Chocolate Madelines, All Other Pastries)",
            "Make sure to hang sticker gun back on cart & plug handheld back in."
        )

    var checkBoxStateArray = SparseBooleanArray()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        val context = parent.context
        val inflater = LayoutInflater.from(context)
        val view = inflater.inflate(R.layout.checkbox_row, parent, false)

        return ViewHolder(view)
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.checkbox.isChecked = checkBoxStateArray.get(position, false)

        holder.checkbox.text = titles[position]

    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var checkbox: CheckBox = itemView.checkbox

        init {

            checkbox.setOnClickListener {
                if (!checkBoxStateArray.get(adapterPosition, false)) {
                    checkbox.isChecked = true
                    checkBoxStateArray.put(adapterPosition, true)
                } else {
                    checkbox.isChecked = false
                    checkBoxStateArray.put(adapterPosition, false)
                }

            }
        }
    }
}

CodePudding user response:

You'll need to persist that data somehow, and restore it when the activity starts. There's lots of ways to do it - a modern approach would be something like a ViewModel handling the internal state (list items and whether they're checked) and pushing that to the UI layer (the fragment, setting those items on the adapter). And whenever you check a box, that event is sent to the VM so it can update the "what's checked" state, and save it to a database, SharedPreferences, DataStore etc.

But if you want to keep it simple, you could just use SharedPreferences and serialise the indices of the checked items. Store them in something like onStop, and restore them during setup, when you're providing data to the adapter. I already wrote an answer about this so I'll just link that, but if there's anything you're not sure of you can just ask here!

CodePudding user response:

You can use SharedPreferences or the SQLite database.

You can also just use a Singleton object class (a static object) with all the ids checked.

HOW TO DECLARE SINGLETON IN KOTLIN

Or you can also pass the checked ids between activities using Intents.

Or you can use LiveData:

LINK

  • Related