Android LiveData re-observed twice when returning to fragment

14 Views Asked by At

This is the code from my Fragment:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ...

        viewModel.myLiveData.observe(viewLifecycleOwner) { data ->
           ...
        }
        ...
}

I noticed that when I come back to my fragment from others fragments (example back navigation) myLiveData is automatically refreshed. In addition onViewCreated also is re-called, so the code inside the observe is executed twice.

Why? viewLifecycleOwner is not correct ?

0

There are 0 best solutions below