Using Lifecycle to manage the Lifecycle of Activity and Fragment components can reduce repetitive boilerplate code and unnecessary hassle.
Lifecycle issues in development
Suppose we want to implement a location function, we need to add the location SDK start and stop methods to the Activity lifecycle callback method:
public class LocationActivity extends AppCompatActivity{ private LocationListener listener; @override protected void onCreate(Bundle savedInstanceState) {new LocationListener(this); listener.onLocationUpdate(newLocationCallback(){@override public void onUpdate(Position Position){// Update UI}}); } @Override protected voidonstart(){
listener.start();
}
@Override
protected void onStop(){ listener.stop(); }}Copy the code
This is a very common approach, but there are some disadvantages:
-
Every time you use the location SDK, you need to write the start and stop methods in the lifecycle, and there is a lot of redundant code if you have multiple activities using the SDK.
-
Lifecycle methods put a lot of code in them, which makes them difficult to maintain.
Using the Lifecycle
Lifecycle is very simple to use. Look at the LocationListener code:
public class LocationListener implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void start(){// start the location service} @onlifecycleEvent (Lifecycle.event.ON_STOP) public voidstop(){// Stop location service}}Copy the code
As you can see from the above code, we simply implemented the LifecycleObserver interface on top of the original code and annotated the start() and stop() methods with lifecycle times that trigger them, with parameters corresponding to the activity lifecycle.
The next step is to make it easier to use in an activity.
public class LocationActivity extends AppCompatActivity{ private LocationListener listener; @override protected void onCreate(Bundle savedInstanceState) {new LocationListener(this); listener.onLocationUpdate(newLocationCallback(){@override public void onUpdate(Position Position){// Update UI}}); getLifecycle().addObserver(listener); }}Copy the code
To achieve the initial positioning function.
Custom Lifecycle components
Activities and Fragments provide the default getLifecycle() method in Support Library 26.1.0, but what if we want to implement the lifecycle in earlier versions and in non-activity and fragment classes? The method is as follows:
public class MyActivity extends Activity implements LifecycleOwner {
private LifecycleRegistry mLifecycleRegistry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLifecycleRegistry = new LifecycleRegistry(this);
mLifecycleRegistry.markState(Lifecycle.State.CREATED);
}
@Override
public void onStart() {
super.onStart();
mLifecycleRegistry.markState(Lifecycle.State.STARTED);
}
@NonNull
@Override
public Lifecycle getLifecycle() {
returnmLifecycleRegistry; }}Copy the code
Using the principle of
There are several important classes throughout Lifecycle:
-
Lifecycle abstract class, which defines some abstract methods to operate LifecycleObserver, as well as State and Event enumeration classes representing the Lifecycle.
-
LifecycleObserver interface, with no internal methods defined, Lifecycle.addobServer () can be registered to listen for Lifecycle changes, with annotated method declarations.
-
LifecycleOwner interface, which is implemented by the activity/fragment and has only one Lifecycle getLifecycle() method.
-
LifecycleRegistry class, which is a subclass of Lifecycle, defines the implementation details internally. By default, a LifecycleRegistry should be declared within a host that implements the LifecycleOwner interface. And set various states for it in its own lifecycle callback method:
this.mLifecycleRegistry.markState(State.CREATED);
Copy the code