Skip to content

Instantly share code, notes, and snippets.

@cesclong
Created March 19, 2021 06:51
Show Gist options
  • Save cesclong/2bf07599641df1d1e5e3b31b54cc3d4b to your computer and use it in GitHub Desktop.
Save cesclong/2bf07599641df1d1e5e3b31b54cc3d4b to your computer and use it in GitHub Desktop.
Support 版本中的具有生命周期的Fragment
public class LifecycleFragment extends Fragment implements LifecycleOwner {
private final LifecycleRegistry registry = new LifecycleRegistry(this);
public LifecycleFragment() {
}
@NonNull
public Lifecycle getLifecycle() {
return this.registry;
}
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.registry.handleLifecycleEvent(Event.ON_CREATE);
}
public void onStart() {
super.onStart();
this.registry.handleLifecycleEvent(Event.ON_START);
}
public void onResume() {
super.onResume();
this.registry.handleLifecycleEvent(Event.ON_RESUME);
}
public void onPause() {
super.onPause();
this.registry.handleLifecycleEvent(Event.ON_PAUSE);
}
public void onStop() {
super.onStop();
this.registry.handleLifecycleEvent(Event.ON_STOP);
}
public void onDestroy() {
super.onDestroy();
this.registry.handleLifecycleEvent(Event.ON_DESTROY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment