mutablelivedata

How to stop LiveData event being triggered more than Once

泄露秘密 提交于 2019-12-11 08:11:54
问题 I am using MutableLiveData within my application for event based communication. I have single activity two fragments architecture. With the help of ViewModel, I'm consuming the LiveData events in Fragment-1. But, when I replace this Fragment-1 with Fragment-2 using Menu bar and finally come back to Fragment-1, old values of LiveData are captured again. How to avoid this problem? Any help/suggestions are highly appreciated! Thank you. 回答1: You can use Event to wrap LiveData values to handle

How to observe changes in database in order to update LiveData

一世执手 提交于 2019-12-05 22:36:09
I am migrating an app from a LoaderManager with Callbacks to an implementation using ViewModel and LiveData . I would like to keep using the existing SQLiteDatabase . The main implementation works OK. The Activity instantiates the ViewModel and creates an Observer which updates the View if it observes changes in the MutableLiveData that lives in the ViewModel . The ViewModel gets it data (cursor) from the SQLiteDatabase through a query using a ContentProvider . But I have other activities that can make changes to the database, while MainActivity is stopped but not destroyed. There is also a

Android LiveData prevent receive the last value when subscribe

帅比萌擦擦* 提交于 2019-11-28 00:29:32
Is it possible to prevent LiveData receive the last value when start observing? I am considering to use LiveData as events. For example events like show message, a navigation event or a dialog trigger, similar to EventBus. I found something similar SingleLiveEvent - but it work only for 1 observer and not for multiple observers. I created a new Class that will hold my real data and a "special ID": class LiveDataItem { long mRealtimeNanos; YOUR_PREVIOUS_LIVEDATA_TYPE mData; LiveDataItem(YOUR_PREVIOUS_LIVEDATA_TYPE data, long realtimeNanos) { this.mRealtimeNanos = realtimeNanos; this.mData =

Android LiveData prevent receive the last value on observe

笑着哭i 提交于 2019-11-26 18:26:43
问题 Is it possible to prevent LiveData receive the last value when start observing? I am considering to use LiveData as events. For example events like show message, a navigation event or a dialog trigger, similar to EventBus. The problem related to communication between ViewModel and fragment, Google gave us LiveData to update the view with data, but this type of communication not suitable when we need update the view only once with single event, also we cannot hold view's reference in ViewModel