Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

667
Views
Android MVVM - Update ViewModel when data changes

I'm working on an app using the MVVM pattern with RxJava. The architecture is the following: enter image description here

It's the first time i use this pattern and i'm not sure about the best way to update a ViewModel (and consequently the corresponding View) when a change occurs in the data, made by another component of the application.

For example: suppose we have an Activity showing the list of users i follow (like a social app), from this list i select a user and open his profile in another Activity. Now, from this second Activity i decide to unfollow the user and when i press the back button to return to the first Activity i would like the list to be updated automatically (deleting the corresponding user, obviously without having to re-download all the data).

The problem is that the two Activity have two different ViewModel. How can i make the changes made by the second Activity affect the ViewModel of the first one? Is it the responsibility of the Repository to inform the first Activity of the changes?

  • I'd rather not use startActivityForResult
  • I'd rather not to inject the ViewModel of the first Activity in the second one

Thanks a lot!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

i decide to unfollow the user and when i press the back button to return to the first Activity i would like the list to be updated automatically (deleting the corresponding user, obviously without having to re-download all the data).

The problem is that the two Activity have two different ViewModel.

I thought you have a Repository that wraps a "Model" (local datasource) that is able to expose LiveData<*>, no?

In which case all you need to do is this:

@Dao
public interface ItemDao {
    @Query("SELECT * FROM ITEMS")
    LiveData<List<Item>> getItemsWithChanges();

    @Query("SELECT * FROM ITEMS WHERE ID = :id")
    LiveData<List<Item>> getItemWithChanges(String id);
}

Now your repository can return LiveData from the DAO:

public class MyRepository {
    public LiveData<List<Item>> getItems() {
        // either handle "fetch if needed" here, or with NetworkBoundResource
        return itemDao.getItemsWithChanges();
    }
}

Which you get in your ViewModel:

public class MyViewModel extends ViewModel {
    private final LiveData<List<Item>> items;

    public MyViewModel(MyRepository repository) {
        this.items = repository.getItems();
    }

    public LiveData<List<Item>> getItems() {
        return items;
    }
}

And if you observe this, then when you modify the item in Room, then it'll automatically update this LiveData in onStart (when you start observing again).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!