- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很难理解 LiveData
是 LifecycleObserver
和 Observer
之间的区别 LiveData
对象在 observe()
方法中接收。
LiveData
是一个 LifecycleObserver
并且因此它知道 Activity
是否正确?
并且 observe()
方法中接收到的 Observer
与 LifecycleObserver
无关,因为它只是用来执行 onChange()
方法,当 LiveData
发生变化时?
最佳答案
Am I right saying that the
LiveData
is aLifecycleObserver
and so it knows about theActivity
because of it?
是的。
And that the
Observer
received in theobserve()
method has nothing to do withLifecycleObserver
because it is there just to execute theonChange()
method when there is a change inLiveData
?
是的。
顾名思义,LifecycleObserver
应该跟踪其父级(Activity
或 Fragment
或任何其他 LifecycleOwner
),使 LiveData
生命周期感知。
另一方面,Observer
跟踪此 LiveData
对象的 value
发生的变化。
所以你是对的。 Observer
与 LifecycleObserver
无关。
关于android - Observer 和 LifecycleObserver 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50660304/
没有调用观察者方法。我按照老派的方式测试了 ViewPagerCycler 并且工作完美。感谢帮助。 public final class ViewPagerCycler implements Lif
我正在处理一个使用架构组件的项目,我对正确使用 LifecycleObserver 模式有疑问。 问题是:我什么时候应该取消注册观察者,如果没有调用取消注册会发生什么? 简单直接的用例如下所示: pu
使用 LifecycleObserver 制作生命周期感知组件非常容易,例如当用户离开屏幕时暂停和停止 MediaPlayer。 但是有什么方法可以让我知道生命周期是否正在经历 onPause、onS
安卓世界。当我尝试运行该应用程序时,它会记录返回此错误 Error:(51, 26) error: cannot access LifecycleObserver class file for and
Google 在 Android 上弃用 Fragment 的 onActivityCreated() 并推荐使用 LifeCycleObserver: To get a callback spec
阅读ViewModel overview ,我想知道 ViewModel 也可以同时成为 LifecycleObserver 吗?我这样做的目的是在用户不与 UI 交互时断开 Firebase 监听器
我正在编写一个(尝试)遵循 MVVM 设计模式的应用程序。我想从该层的其他部分观察模型层的变化。例如 假设我正在使用 room 从我的数据库中公开对象列表: @Dao interface MyDao
我很难理解 LiveData 是 LifecycleObserver 和 Observer 之间的区别 LiveData 对象在 observe() 方法中接收。 LiveData 是一个 Lifec
我的 ViewModel 类实现了 LifecycleObserver。当我调用 fragment.lifecycle.addObserver(this) 时,它会产生异常。 Caused by: j
我已阅读有关 Life Cycle 的文档和 Live Data在android官方文档中。我知道该类实现了 LifeCycleObserver 并使位置监听器自动关闭或打开。我也知道实时数据可以自动
我目前使用 LifecycleObserver 来通知我的 View 的事件,例如 onStart。这对于作为演示者或处理程序的组件非常有用,例如 FacebookLoginHandler 需要在 V
我是一名优秀的程序员,十分优秀!