![](/img/trans.png)
[英]Android Architecture Components - ViewModel Observable & Proguard
[英]Android Architecture Components - Observable not triggered
我有一個運行架構組件的簡單應用程序。 自從庫的最新更新(“ Beta2”)以來,我的觀察對象沒有被觸發存在問題(在Fragment中,相同的代碼適用於Activity)
這是當前無法使用的示例。
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()
})
}
}
我錯過了什么嗎?
謝謝你的幫助。
我終於找到了解決上述問題的方法。
由於我們處在片段中,因此我必須使用Activity進行觀察。 (它應該是“ this”,但到目前為止還沒有奏效。
isDataReady.observe(activity, Observer {
我想這是一個臨時解決方案...
謝謝你的幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.