【发布时间】:2019-10-17 18:44:13
【问题描述】:
我需要让 LifecycleOwner 在我的 FirestoreRecyclerViewOption 中传递它,但 Fragment 没有实现 Lifecycle。
那么,如何获得呢?
My Fragment 实现 LifecycleOnwer 如下代码示例所示,
public class ListRestFragment extends Fragment implements LifecycleOwner
在onCreateMethod之后,
lifecycleRegistry = new LifecycleRegistry(this);
lifecycleRegistry.setCurrentState(Lifecycle.State.CREATED);
结束
@Override
public void onStart() {
super.onStart();
lifecycleRegistry.setCurrentState(Lifecycle.State.STARTED);
}
@NonNull
@Override
public Lifecycle getLifecycle() {
return lifecycleRegistry;
}
我按照这个例子 但它不起作用
Attempt to invoke virtual method 'androidx.lifecycle.Lifecycle$State androidx.lifecycle.Lifecycle.getCurrentState()' on a null object reference
我需要它来编辑 My FirestoreRecyclerViewOption 和 setLifeCycleOwner,因为它始终为 null,而且我每次都有一个 NullPointer。
【问题讨论】:
-
你在使用已弃用的框架 Fragments 吗?所有支持库/AndroidX 片段都已经实现了 LifecycleOwner。
-
@ianhanniballake 我的应用是基于 Api 19 构建的。
-
好的,那么您使用的是什么 Fragments?支持库片段支持回到 API 14
-
@ianhanniballake 我使用 androidx.fragment.app
-
那么他们已经按照the documentation实现了LifecycleOwner。
标签: android android-lifecycle fragment-lifecycle