使用 viewModel 时应该添加 binding.lifecycleOwner=this 吗?
Should I add binding.lifecycleOwner=this when I use viewModel?
在我看来,我应该在使用viewModel
的时候加上binding.lifecycleOwner=this
。
我发现很多项目比如Code A没有加binding.lifecycleOwner=this
,为什么?
代码A来自项目https://github.com/enpassio/Databinding
代码A
class AddToyFragment : androidx.fragment.app.Fragment() {
private lateinit var binding: AddToyBinding
...
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = DataBindingUtil.inflate(
inflater, R.layout.fragment_add_toy, container, false
)
setHasOptionsMenu(true)
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
(requireActivity() as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true)
//If there is no id specified in the arguments, then it should be a new toy
val chosenToy : ToyEntry? = arguments?.getParcelable(CHOSEN_TOY)
//Get the view model instance and pass it to the binding implementation
val factory = AddToyViewModelFactory(provideRepository(requireContext()), chosenToy)
mViewModel = ViewModelProviders.of(this, factory).get(AddToyViewModel::class.java)
binding.viewModel = mViewModel
binding.fab.setOnClickListener {
saveToy()
}
binding.lifecycleOwner=this //I think it should add
}
binding.lifecycleOwner
用于通过数据绑定观察 LiveData
。
有点像 android:text=@{viewModel.text}
,其中 val text:LiveData<String>
。视图将在运行时观察文本变化。
据我了解,
binding.lifecycleOwner= this
一方面用于在 liveData 更改时订阅以接收消息(以便信息在视图中保持一致),另一方面用于在视图和片段被销毁时从列表中删除观察者(以防止内存泄漏) ). Fragment 的 viewLifecycleOwner 更适合作为 lifecycleOwner 以这种方式绑定,不是吗?
在我看来,我应该在使用viewModel
的时候加上binding.lifecycleOwner=this
。
我发现很多项目比如Code A没有加binding.lifecycleOwner=this
,为什么?
代码A来自项目https://github.com/enpassio/Databinding
代码A
class AddToyFragment : androidx.fragment.app.Fragment() {
private lateinit var binding: AddToyBinding
...
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = DataBindingUtil.inflate(
inflater, R.layout.fragment_add_toy, container, false
)
setHasOptionsMenu(true)
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
(requireActivity() as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true)
//If there is no id specified in the arguments, then it should be a new toy
val chosenToy : ToyEntry? = arguments?.getParcelable(CHOSEN_TOY)
//Get the view model instance and pass it to the binding implementation
val factory = AddToyViewModelFactory(provideRepository(requireContext()), chosenToy)
mViewModel = ViewModelProviders.of(this, factory).get(AddToyViewModel::class.java)
binding.viewModel = mViewModel
binding.fab.setOnClickListener {
saveToy()
}
binding.lifecycleOwner=this //I think it should add
}
binding.lifecycleOwner
用于通过数据绑定观察 LiveData
。
有点像 android:text=@{viewModel.text}
,其中 val text:LiveData<String>
。视图将在运行时观察文本变化。
据我了解,
binding.lifecycleOwner= this
一方面用于在 liveData 更改时订阅以接收消息(以便信息在视图中保持一致),另一方面用于在视图和片段被销毁时从列表中删除观察者(以防止内存泄漏) ). Fragment 的 viewLifecycleOwner 更适合作为 lifecycleOwner 以这种方式绑定,不是吗?