我需要让LifecycleOwner在我的FirestoreRecyclerViewOption中传递它,但Fragment无法实现Lifecycle。
那么,如何获得?
我的片段实现了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。
该片段本身就是您正在寻找的LifecycleOwner。您可以使用关键字“ this”作为对该片段的引用。
阅读此:“Lifecycle是一个类,其中包含有关组件生命周期状态(例如活动或片段)的信息,并允许其他对象观察此状态。“
和:“ LifecycleOwner是一个单一方法接口,表示该类具有生命周期。”