The question is the following: I have a RecyclerView list, where there is a button next to each item (with a picture). When you press a button, the image changes. I need to save the state of the button (and the picture itself), if the person pressed the button, and so that after exiting the Activity and returning back, the button remains in the state in which it was before the exit. What are the ways and solutions to this issue?

This is how I change the picture and the state of the button:

In ViewHolder I specified the boolean variable, which I set to false , and then used it in OnBindViewHolder .

 public void onBindViewHolder(@NonNull final ProductViewHolder holder, final int position) { holder.textProduct.setText(list.get(position).getProductName()); holder.btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(holder.click==false) { MainMenuActivity.basketList.add(list.get(position).getProductName()); holder.btn.setBackgroundResource(R.drawable.delete); holder.click=true; } else { holder.btn.setBackgroundResource(R.drawable.plus); holder.click=false; MainMenuActivity.basketList.remove(list.get(position).getProductName()); } } }); } 
  • 2
    There is only one way - to write the current state to the external storage (as a database), and the next time you start to read from it. - pavlofff 11:44 pm

1 answer 1

  1. The best option, as indicated in the comment, is to save to the database or notify the server of changes if the list is received from the server.
  2. If the project does not contain a database, then you can change the elements of this sheet and transfer the sheet between activations, putting it in Intent, but this is not a very good solution, since if both activations are destroyed by the user - changes in the list will be lost
  • Answer to the first answer: I use Firebase Realtime Database in my application. Do you have any options for how to do this using this database? The answer to the second question: you can transfer using putExtra. But the question is: where do I get the values ​​of the list, record the status and transmit? - Famous
  • This can help techotopia.com/index.php/... you must either overwrite all the data in the onStop activation method, or a specific item at each button state change and based on the button state - show a picture - Dmitry Grishin
  • I'm sorry, but the link is not working. He writes "Currently there is no text on this page. You can search for the title of this page on other pages or search for relevant journals, but you do not have permission to create this page." - Famous
  • try again, there is a word attached to the link - Dmitry Grishin
  • And you can tell more about the second way to tell. What values ​​should I read and write to the list, then transfer it to another activation via putExtra? How can I overwrite the values ​​when I re-enter the list? I just do not really understand how, because new to Android. - Famous