简体   繁体   English

Android ViewModel LiveData 文档

[英]Android ViewModel LiveData Documentation

I read the documentation but became confuse on some part.我阅读了文档,但在某些方面感到困惑。

ViewModel objects are designed to outlive specific instantiations of views or LifecycleOwners. ViewModel 对象旨在比视图或 LifecycleOwners 的特定实例更长寿。 This design also means you can write tests to cover a ViewModel more easily as it doesn't know about view and Lifecycle objects.这种设计还意味着您可以更轻松地编写测试来覆盖 ViewModel,因为它不了解视图和生命周期对象。 ViewModel objects can contain LifecycleObservers, such as LiveData objects. ViewModel 对象可以包含 LifecycleObservers,例如 LiveData 对象。 However ViewModel objects must never observe changes to lifecycle-aware observables, such as LiveData objects.然而,ViewModel 对象绝不能观察生命周期感知可观察对象的变化,例如 LiveData 对象。 If the ViewModel needs the Application context, for example to find a system service, it can extend the AndroidViewModel class and have a constructor that receives the Application in the constructor, since Application class extends Context.如果 ViewModel 需要 Application 上下文,例如查找系统服务,它可以扩展 AndroidViewModel class 并在构造函数中具有接收应用程序的构造函数,因为 Application class 扩展了上下文。

This part is somewhat confusing for me这部分让我有些困惑

However ViewModel objects must never observe changes to lifecycle-aware observables, such as LiveData objects.然而,ViewModel 对象绝不能观察生命周期感知可观察对象的变化,例如 LiveData 对象。

Is this referring to this kind of implementation?这是指这种实现吗?

Fragment分段

class AboutFragment : Fragment() {

    private lateinit var aboutViewModel: AboutViewModel
    private var _binding: FragmentAboutBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View {
        aboutViewModel =
                ViewModelProvider(this).get(AboutViewModel::class.java)

        _binding = FragmentAboutBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val textView = binding.aboutTxt

        //Observe changes
        aboutViewModel.text.observe(viewLifecycleOwner, {
            textView.text = it
        })
        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

ViewModel视图模型

class AboutViewModel : ViewModel() {

private val _text = MutableLiveData<String>().apply {
    value = "Foobar......"
           
}

fun setText(text: String){ _text.value = text}

val text: LiveData<String> = _text

} }

Or this is what they mean LiveData<LiveData<Object>> not to do?或者这就是他们的意思LiveData<LiveData<Object>>不这样做?

it means you should never use observe on a livedata object in your viewmodel, only in activity/fragment like you are already doing这意味着您永远不应在视图模型中的 livedata object 上使用observe ,仅在您已经在做的活动/片段中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM