B
B
beem72021-05-21 01:09:01
Android
beem7, 2021-05-21 01:09:01

How to store data in memory between different activities with the ability to observe?

Here we have an activity with a certain list. which the user can edit. So, the architecture is as follows: List (it contains data), RecyclerView and RecyclerView.Adapter.

In addition, it is possible to save data in shared preferences and load them from there. We load it in onCreate, and to save we have a function that we pull from the adapter observer, so that it will definitely twitch with any changes. It saves only non-transient fields of models.

Everything is fine. But, when you click on each element of the list, another activity should open. Which also has a button "Download" (file). And this button starts the download. And the result of this download should be displayed both in this activity and in the list activity (in this list element).

And here it is not clear how to be. I can imagine, of course, that the Adapter provides that capability as well. Make a special InMemoryDB class and put the adapter in a public static field of this class. And put the List object itself in the same field. And pull them out of both activities calmly. And even in the InMemoryDB class, write some helpers to make these jerks more DRY and there is less risk of making a mistake.

But it's still spaghetti, and it feels like I'm hammering nails with a microscope, piling on the tool (Adapter) the functionality for which it is not adapted. At least that's why. that Adapter is a RecyclerView. Adapter, and I will just as well make a data store out of it, which are not displayed anywhere in the RecyclerView at all.

How will be correct?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-05-21
@zagayevskiy

In general, everything was correctly told to you about single activity. There must be very good reasons for using more than one activity. Inside, everything is destroyed by fragments or their analogues.
Now, when you switch to fragments, you can make a normal di between them, and not bother.
The adapter is needed only to display data in ui, that's it, it shouldn't do anything else.
See Repository pattern.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question