I ran into such a problem that when I rotate the screen, the data that I added to my list disappears.
I tried to solve this problem through onSaveInstanceState() and onRestoreInstanceState(), or rather through the methods putParcelable() and getParcelable(), but I never figured out how to implement it
(I apologize for the possibly stupid question, I'm just starting to learn android development)
Here is the code of my RecyclerView:
class MainActivity : AppCompatActivity() {
private var id = 0
private lateinit var b : ActivityMainBinding
private val adapter = ItemAdapter()
private lateinit var item : Item
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
b = ActivityMainBinding.inflate(layoutInflater)
setContentView(b.root)
init()
with(b) {
addBtn.setOnClickListener{
var title = edTitle.text.toString()
var img = R.drawable.img
var descr = "1111111111111111111111111"
item = Item(id++, img, title, descr)
adapter.addItem(item)
}
lastBtn.setOnClickListener {
adapter.setTrueCheck(item)
}
resetBtn.setOnClickListener {
adapter.resetCheck()
}
showBtn.setOnClickListener {
adapter.showChecked(applicationContext)
}
}
}
private fun init() {
b.apply {
rcView.layoutManager = LinearLayoutManager(this@MainActivity)
rcView.adapter = adapter
}
}
}
class ItemAdapter : RecyclerView.Adapter<ItemHolder>() {
private var itemList : ArrayList<Item> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
return ItemHolder(view)
}
override fun getItemCount(): Int {
return itemList.size
}
override fun onBindViewHolder(holder: ItemHolder, position: Int) {
holder.bind(itemList[position])
//holder.changeChecked(itemList[position], itemList)
holder.b.itemIsChck.setOnClickListener {
itemList[position].isCheck = !itemList[position].isCheck
onceCheck(itemList[position])
}
}
fun addItem(item : Item) {
itemList.add(item)
notifyDataSetChanged()
}
fun setTrueCheck(item : Item) {
item.isCheck = true
onceCheck(item)
}
fun resetCheck() {
for (i in itemList.indices) {
itemList[i].isCheck = false
}
notifyDataSetChanged()
}
fun onceCheck(item: Item) {
for (i in itemList.indices) {
if (itemList[i].id != item.id) itemList[i].isCheck = false
}
notifyDataSetChanged()
}
fun showChecked(context: Context) {
var msg = ""
for (i in itemList.indices) {
if (itemList[i].isCheck) {
msg += "${itemList[i].id}"
break
}
}
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
}
}
class ItemHolder(item : View) : RecyclerView.ViewHolder(item) {
val b : ItemLayoutBinding = ItemLayoutBinding.bind(item)
fun bind(item: Item) = with(b) {
itemImg.setImageResource(item.image)
itemTitle.text = item.title
itemDescr.text = item.descr
itemIsChck.isChecked = item.isCheck
}
}
class Item(
val id : Int,
var image : Int,
var title : String,
var descr : String,
var isCheck : Boolean = false
)
Data loss on screen rotation occurs due to the Android activity lifecycle. When the screen is rotated, the following sequence of events happens:
Your idea with
onSaveInstanceStateetc. isn't a bad one, it would work. However, recommended way to prevent that is using ViewModel, which makes the thing simpler. I suggest reading this article (which also mentions the problem you have): click me.To better understand the concept of view models I strongly recommend this video as well: MVVM explained. I think it's a pretty good foundation, then you can explore further :)