Наблюдатель LiveData никогда не удаляется между общей ViewModelAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Наблюдатель LiveData никогда не удаляется между общей ViewModel

Сообщение Anonymous »


I'm becoming crazy.

I have a SharedViewModel shared between FragmentA and FragmentB, which contains a LiveData:

class SharedViewModel : ViewModel() { private val _searchParameters = MutableLiveData(SearchParameters()) val searchParameters: LiveData = _searchParameters } and I observe this liveData in the onViewCreated() of my FragmentA (only, not in FragmentB):

viewModel.searchParameters.observe(viewLifecycleOwner) { searchAndFilter -> Log.d("FragmentA", "observed searchParameters in FragmentA") mSearchAdapter.updateSearchParameters(searchAndFilter) } My problem is that the observer never gets removed when FragmentA is destroyed:

If I navigate to FragmentB (using Navigation Component), then from FragmentB I do findNavController().popBackStack(), I would expect FragmentA to have removed my observer in onDestroyView() (which I have confirmed is called correctly when navigating to FragmentB). But this isn't happening. So when I navigate back to FragmentA, the LiveData is being observed another time, and now I have multiple observations of this LiveData. If I navigate back and forth 10 times, my observer has now been registered 10 times and is being called 10 times foe every event.

I have also tried removing the observer manually by keeping a reference to it and doing

override fun onDestroyView() { super.onDestroyView() Log.d("FragmentA", "onDestroyView") viewModel.searchParameters.removeObserver(searchParametersObserver) } But the same problem occurs.

So my question is what is going on, and how can I fix this problem?


Источник: https://stackoverflow.com/questions/780 ... -viewmodel
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»