I have a single Activity application. In one part of the app there is a master-detail combination of Fragments that use a shared ViewModel to share some data between them. The problem I face is that the ViewModel's onCleared() method is not called even when both of the Fragments have been destroyed. onCleared() is only called when the activity is destroyed.
Is this how it is supposed to work? Because that is quite useless in a single Activity model, because the Activity is always alive. Or am I missing something?
requireActivity()
I passparentFragment
, but the result is the same, since the parent is theActivity
for all theFragments
. If I pass the fragments themselves then there is no sharing of data, because each Fragment creates it's ownViewModel
. I guess a sharedViewModel
is not a solution to my initial problem. – Beccafico