Why LiveData observer is being triggered twice for a newly attached observer

后端 未结 12 2068
北荒
北荒 2020-12-12 18:03

My understanding on LiveData is that, it will trigger observer on the current state change of data, and not a series of history state change of data.

12条回答
  •  庸人自扰
    2020-12-12 18:36

    The reason is that in your .observe() method, you passed a fragment as the lifecycle owner. What should have been passed is the viewLifecycleOwner object of the fragment

    viewModel.livedata.observe(viewLifecycleOwner, Observer {
            // Do your routine here
        })
    

提交回复
热议问题