Home > Enterprise >  how to save state of each item in recycleview?
how to save state of each item in recycleview?

Time:03-08

Here is the structure of the application

Main Activity --> movies fragment/series fragment --> recycle view via custom adapter --> card view XML layout

the main activity dynamically selects the fragments based on the button selection. the movies/series fragments use the adapter to initialize the recycler view. the adapter uses an XML card view layout for each item which also contains the like and dislike radio buttons.

so here is my question, how do I save the state of the like and dislike radio buttons? when switching between pages(fragments)?

I've attached an image of the application for more context

enter image description here

every time I click movies or series button the fragments rerenders and loses all information.

do I save the state in the main activity? the movies fragment? or in the adapter? whats the correct approach?

CodePudding user response:

  1. You can't save state like this because hard coded object will be destroyed and created on app exit and start. I think best approach will be to use room database
  2. Add a boolean(Like /disliked to your data class.
  3. Use room database to save your data in storage.

CodePudding user response:

A simple and faster solution is shareprefs for each item ID. But this is not recommended for real-world projects Either you can use Room for the local database or you can update every like or unlike on the server and then fetch your data from the server when fragment recreates. all solution depends on your use case. factors to keep in mind is size of data-list and update frequency of your data.

  • Related