我需要让 LifecycleOwner 在我的 FirestoreRecyclerViewOption 中传递它,但 Fragment 没有实现生命周期。
那么,如何获得呢?
我的 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
我需要它来编辑我的 FirestoreRecyclerViewOption 和 setLifeCycleOwner,因为它始终为 null,而且我每次都有一个 NullPointer。
最佳答案
在 fragment 中,您可以使用 getViewLifecycleOwner()
方法来获取 lifeCycleOwner。
关于android - 如何在 Fragment 中获取 LifecycleOwner?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58438949/