Storing data in a fragmentfrom the CommonsWare Community archives
At May 8, 2019, 10:48am, Pvr asked:
- How do I save data(in 2nd fragment) that I pass from one fragment to another , I have Recycler view in 1st fragment.
At May 8, 2019, 11:03am, mmurphy replied:
If it is data that can go in a
Bundle, put the data in the arguments
setArguments(). That way, the data will be retained across configuration changes automatically.
If it is data that cannot go in a
Bundle… ideally you do not pass that data between fragments. Instead, you have a repository or other central source of that data, and you pass identifiers between fragments (where those identifiers can go in a
Bundle). For example, in the app that we build in Exploring Android, we do not pass a
ToDoModel between fragments — we pass an ID value between fragments, and we get the
ToDoModel from the
ToDoRepository based on its ID.