[英]Android Architecture Components - Observable not triggered
I've a simple application running Architecture Components. 我有一个运行架构组件的简单应用程序。 Since the latest update of the library ("Beta2"), i've an issue with my observable not being triggered (in a Fragment, the same code works on an Activity)
自从库的最新更新(“ Beta2”)以来,我的观察对象没有被触发存在问题(在Fragment中,相同的代码适用于Activity)
Here is a sample which is currently not working. 这是当前无法使用的示例。
class SampleFragment : Fragment() {
private var isDataReady = MutableLiveData<Boolean>()
private val registry = LifecycleRegistry(this)
override fun getLifecycle(): LifecycleRegistry = registry
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main2, container, false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fab.setOnClickListener { _ ->
isDataReady.postValue(true)
}
isDataReady.observe(this, Observer {
Snackbar.make(fab, "Hello!", Snackbar.LENGTH_LONG).show()
})
}
}
Did i missed something ? 我错过了什么吗?
Thanks for your help. 谢谢你的帮助。
I've finally figured out a solution on the above issue. 我终于找到了解决上述问题的方法。
As we are in a Fragment, i must use the Activity to observe. 由于我们处在片段中,因此我必须使用Activity进行观察。 (It should be "this" but it's not working so far.
(它应该是“ this”,但到目前为止还没有奏效。
isDataReady.observe(activity, Observer {
I guess it's a temporary fix... 我想这是一个临时解决方案...
Thanks for your help 谢谢你的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.