引入androidx后,ViewModel+LiveData搭配Activity/Fragment渐渐成为大家喜爱(习惯)的UI制作方式。 总体来说,这套模式的容易学习,使用也方便,但如果没有详细了解其背后的工作机制,也很容易错误使用造成bug。 在此将分享相关architecture component背后原理,将实际开发中遇到的问题和错误总结一些good practice points,以供参考。 这篇文章主要探讨LiveData的 工作原理 和 基本使用方式。 How it works: LiveData::setValue VS LiveData::postValueLiveData::observe(lifecycleOwner, observer)Good Practice Points: Activity观察LiveData,在onCreate注册观察,livedata.observe(this, Observer<T>