How to update RecyclerViews in Android in a ClientServer architecture with single activity and mutiple fragments

Viewed 184

I would like to create an App that has clients and a server (so basically they are 2 different apps). In each of the Clients (and the servers) I use a single-activity-multiple-fragments approach and in each App I have a SQLite database. In some fragments I use a RecyclerView. Now the server should be able to update the recylerview in each individual client (and the database) separately and vice versa, meaning that the clients should be able to trigger an update of the recyclerView and databases of the server.

Now my question is, how can I generally do this in Android? I did an Internet search and often for communication within an app between different fragments the ViewModel and LiveData approach is used. Now I am wondering, if I can also use these approaches for my purposes? I also found an official Android developer page about communcation between Apps https://developer.android.com/training/basics/intents but here they are using intents and I think that they do not use the single-activity-multiple-fragments approach.

So is it possible to use the ViewModel and LiveData approach for that or do I have to use the intent-approach?

Update: I was told that I can't use the ViewModel or LiveData for my purposes. Do you have an idea how I can do what I want? So the clients should be able to send something to the server and the server should be able to send information to the database of the clients and based on that the clients should update their Recyclerview. Can I use sockets for that?

Does nobody have a further idea how I can implement this? As I am struggeling on this question I would highly appreciate your comments and would be quite thankful for your help.

Here you can see a rough sketch of the app architecture.enter image description here

1 Answers

In fact, the answer to your question does not fit in one text and requires various articles with details. So I just mentioned some links/articles about the implementation of each part. Also, I try to look at the issue from a higher perspective. And then take a closer look at each part.

I separate your problem into two-part:

1. Infrastructure: Communicating data between client and server

According to your comments, these apps are not installed on the same device, but they're in a network and can ping together and the communicated messages are structural data (order of a dish), you have at least 2 options:

  1. If the apps have access to the Internet I suggest using Firebase Realtime Database:

The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. When you build cross-platform apps with our iOS, Android, and JavaScript SDKs, all of your clients share one Realtime Database instance and automatically receive updates with the newest data.

also, Google has provided a chat project as codelabs. another example

  1. Using java socket to run the socket server and connect to it. article1 , article2

2. Presentation: reading data and update UI

After implementing send/receive messages, we can use ViewModel and keep messages in a LiveData. And then observe the livedata in fragment/activity and show messages in the RecyclerView.

Google also has provided a couple of codelabs about these subjects too:

Related