为什么即使生命周期所有者的状态不同于STARTED或RESUMED

时间:2019-08-10 15:01:28

标签: android android-lifecycle android-architecture-components android-livedata

我有一个recyclerview,我想更新在视图模型上分配的LiveData中的白色数据。

因此,我使用Activity.onCreateLiveData.observe method方法创建了一个观察者

通读LiveData.observe方法的文档,我发现以下内容:

     * The observer will only receive events if the owner is in {@link Lifecycle.State#STARTED}
     * or {@link Lifecycle.State#RESUMED} state (active).

基于此,我想记录我正在使用的生命周期所有者的状态:

Log.d("JimmyDebug:", "life cycle owner state ${this.lifecycle.currentState}")
        plantGroupListViewModel.plantsData.observe(this, Observer<ListPlantState> {
            when (it) {
                is PlantGroupList -> {
                    plantGroupListAdapter.setData(it.plantGroupList)
                }
            }
        })

注意:此代码是通过Activity.onCreate方法调用的

结果是:

08-10 09:35:11.308 10322-10322/com.blundell.mygarden D/JimmyDebug:: On observer
    life cycle owner state INITIALIZED

基于此,我不明白为什么观察者实际上接收到处于INITIALIZED状态的事件,基本上观察者可以正常工作

您知道为什么要这么做吗?

0 个答案:

没有答案