How to notify observer when adding an item to a LiveData list?
Hi! I'm new to Android development in Kotlin and trying to understand the MVVM architecture. I have a ViewModel that stores a list of items (e.g., tasks) in LiveData
- >. I want that when a new item is added to this list, all subscribed observers (e.g., Activity or Fragment) receive a notification and update the UI.
The problem is that LiveData does not track changes inside the list itself. If I just do myList.add(newTask), the observers don't trigger because the reference to the list object remains the same. I tried calling postValue() and setValue() after adding, but that doesn't help.
Here's an example of my code:
```kotlin
class TaskViewModel : ViewModel() {
private val _tasks = MutableLiveData
- >(mutableListOf())
val tasks
No answers yet.
Log in to leave an answer