Created
March 19, 2021 06:51
-
-
Save cesclong/2bf07599641df1d1e5e3b31b54cc3d4b to your computer and use it in GitHub Desktop.
Support 版本中的具有生命周期的Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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