我有一个生命周期感知片段和一个LifecycleObserver
类
public class MyFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new MyObserver(this);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main, container, false);
}
}
以下是我的观察者类,它记录了所有片段事件的属性
public class MyObserver implements LifecycleObserver {
private static final String TAG = "MyObserver";
public MyObserver(LifecycleOwner lifecycleOwner) {
lifecycleOwner.getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreate(){
Log.d(TAG, "onCreate: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause(){
Log.d(TAG, "onPause: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy(){
Log.d(TAG, "onDestroy: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart(){
Log.d(TAG, "onStart: ");
}
}
我想收听片段特定的生命周期事件onDestroyView
, onActivityCreated
但这些事件不存在
Lifecycle.Event
. 它仅包含活动事件。如何在我的观察者中监听片段事件