https://kotlinlang.org logo
Title
g

Gilles Braun

07/24/2019, 12:14 PM
Hey guys, this is not really kotlin specific but I have a weird issue with a shared viewmodel between an activity and fragment: The activity's observer is only executed once, whereas the fragment's observer is executed on every update. Any ideas why this might be?
r

ruialonso

07/24/2019, 12:28 PM
set observe in Fragment at
onCreate
instead of
onViewCreated
. Fragment lifecycle is different from Activity and doesn't die, so every time Fragment is shown again it will handle observable from viewmodel even if no updates
g

Gilles Braun

07/24/2019, 12:35 PM
I have the observers in
onStart
. Trying to observe the viewmodel in
onCreate
doesn't work as with the error
IllegalStateException: Can't access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView()
. As far as I can tell the problem is not with the fragment, as it receives the updates correctly. But the activity doesn't
r

ruialonso

07/24/2019, 12:41 PM
ok, I thought that problem was fragment observer many times
g

Gilles Braun

07/24/2019, 12:41 PM
The fragment receives every update correctly, but the activity only the first
r

ruialonso

07/24/2019, 12:41 PM
but activity receives observable first time at least, no?
g

Gilles Braun

07/24/2019, 12:41 PM
yes
r

ruialonso

07/24/2019, 12:42 PM
any other changes activity doesn't?
😐
g

Gilles Braun

07/24/2019, 12:43 PM
This is what the log outputs, people is the livedata:
observer called in fragment, people: 1
observer called in activity, people: 1
observer called in fragment, people: 2
observer called in fragment, people: 3
observer called in fragment, people: 4
observer called in fragment, people: 3
r

ruialonso

07/24/2019, 12:44 PM
wow
g

Gilles Braun

07/24/2019, 12:44 PM
it doesn't depend on the livedata itself either, because all other livedata's also only get 1 update when starting
r

ruialonso

07/24/2019, 12:47 PM
are you using a ViewModelProvider or koin to share viewmodel?
g

Gilles Braun

07/24/2019, 12:48 PM
ViewModelProviders
r

ruialonso

07/24/2019, 12:49 PM
I'm sorry @Gilles Braun, have no idea why
g

Gilles Braun

07/24/2019, 12:49 PM
thanks for your time 🙂
r

ruialonso

07/24/2019, 12:50 PM
seems like SingleLiveEvent that don't propagate to more observers
I don't know
g

Gilles Braun

07/24/2019, 12:56 PM
I just found the problem, they were 2 separate viewmodel instances, not the same. The reason it always sent the first update is because Viewmodel initilizes its value.
It works now when I instantiate it like this:
ViewModelProviders.of(getActivity()).get(..)
r

ruialonso

07/24/2019, 12:58 PM
jaja just this
fine 😄
have you tried koin? is simple to share viewmodel and provide them
g

Gilles Braun

07/24/2019, 12:59 PM
This app has already Dagger implemented, so I would have to use that
g

Grygorii Shcherbiak

07/24/2019, 6:11 PM
if you have 2 separate instances of viewModel that might mean you have 2 fragments where you are supposed to have just one. And that is why switching to activity helps. I would suggest to check
this
pointers every time you hit a breakpoint in the LiveData observer and validate this