I have a fragment, which contains LiveData
, and SwiperRefreshLauout
. In onCreateView
I have set the observer for some String.
User, while playing with fragment, can change the value of this string, but it should be only temporary. Whenever user will trigger onRefreshListener
, the String should get "default" data, that came from observer.
I'd like to know, if there's any way to call data from observer again, in onRefreshListener
, or I have to remove observer from onCreateView
, and make another one after onRefreshListener
is called?
Dummy scenario:
We have String, called test. In OnCreateView
, we are setting the observer to the ViewModel
, and get some data from database to the test string. Let's say it'll be name - "Mark". When user will pick from spinner another name: "Carl" the test string will have value of "Carl" now. User would like to refresh the UI of layout, and now, the test string should have value - "Mark" again.
If I understand your situation correctly, here I have a suggestion:
viewModel.data.observe(...)
viewModel.fetchData("initialValue")
. On the other hand, since this is the initial call, you could use the init
section of the View Model class and call there the initial fetch. init { fetchData("initialValue") }
onRefresh
you could call the function to fetch the data, but this time with the temp value viewModel.fetchData("defaultValue")
So, everytime you call the viewModel.fetchData("anyValue")
the data
LiveData will be refreshed and since you are already subscribed in the onCreateView
then you will receive the updates there.
if this is not clear, please share some code in this way, I can give better advise.