LifeCycle
LifeCycle is a component provided by Jetpack that can sense the life cycle changes of an Activity or Fragment. This component is convenient for business to make corresponding management and changes according to the changes of business life cycle, and can also prevent business memory leakage.
First, understand the following concepts related to LifeCycle:
- LifeCycle: abstract class, which defines the abstract methods of adding and deleting an observer (LifeCycle observer), and also defines the LifeCycle event Lifecycle.Event and the LifeCycle state Lifecycle.State;
- Lifecycle.Event: the internal enumeration class of LifeCycle, which defines the LifeCycle events of LifeCycle owner (i.e. Activity or Fragment), from ON_CREATE to ON_DESTROY;
- Lifecycle.State: the internal enumeration class of LifeCycle, which defines the LifeCycle state.
- LifecycleRegistry: the implementation class of LifeCycle, which manages LifecycleObserver and maintains the LifeCycle state LifeCycle State and is responsible for distributing the LifeCycle events of Activity or Fragment Event;
- LifecycleObserver: interface, life cycle observer. It has no method and depends on the annotation onllifecycle event;
- FullLifecycleObserver: interface, which inherits from the LifecycleObserver interface and observes the life cycle of an Activity or Fragment in the form of life cycle methods (onCreate... onDestroy);
- LifecycleEventObserver: interface, which inherits from the LifecycleObserver interface and observes the life cycle of an Activity or Fragment in the form of life cycle events (ON_CREATE... ON_DESTROY);
- LifecycleOwner: interface, associated with Activity or Fragment related life cycle events;
- Onllifecycle event: annotation, which is mainly used to declare methods to listen to lifecycle Event;
- Lifecycle: auxiliary class, which converts LifecycleObserver into adapter LifecycleObserver (fulllifecycleobserver adapter, SingleGeneratedAdapterObserver, compositegeneratedadapterobserver, ReflectiveGenericLifecycleObserver), where ReflectiveGenericLifecycleObserver is processed to annotate the LifecycleObserver declared by onllifecycle event.
After understanding the concepts related to LifeCycle, let's understand its brief class diagram:
In general, the LifecycleObserver defined by the business observes the relevant life cycle of Activity or Fragment by annotating onllifecycle event. When the life cycle of ReportFragment or Fragment changes, reflective genericlifecycleobserver calls the relevant methods of LifecycleObserver with onllifecycle event annotation through reflection.
Here, the monitoring of Activity's life cycle is realized through ReportFragment, and the monitoring of Fragment's life cycle is realized through its own life cycle callback. In addition, the lifecycle events defined and declared in LifecycleObserver are called after the Activity or Fragment's own lifecycle method is called. Because LifecycleRegistry stores lifecycleobservers in a linked list structure Map when adding an observer, the registered related lifecycleobservers are triggered in the order of adding them when they finally accept and process life cycle events.
flow chart:
The LifeCycle process in Activity is:
- Implement the lifecycle owner interface in the Activity and create the lifecycle registry object;
- In ReportFragment, when the SDK of the current version is greater than or equal to 29 (that is, Android 10), the life cycle listener of Activity will be created; otherwise, the life cycle of ReportFragment will be used as the listener;
- When the life cycle of ReportFragment changes, the LifecycleOwner object is obtained through the activity object in ReportFragment, and then the LifecycleRegistry object of the implementation class of Lifecycle is obtained, then its handleLifecycleEvent method is invoked.
- Add the Lifecycle observer LifecycleObserver, obtain the Lifecycle implementation class LifecycleRegistry object through the Activity, call the addObserver method, remove the Lifecycle observer LifecycleObserver, and call removeObserver;
- addObserver method creates an ObserverWithState object and passes the lifecycle observer object to the constructor of ObserverWithState. In this method, an adapter object that implements lifecycle eventobserver is created through lifecycle. The lifecycle observer object is wrapped in the adapter object of lifecycle eventobserver;
- LifecycleRegistry uses a linked list Map to maintain the LifecycleObserver object and ObserverWithState object, and also maintains the lifecycle state lifecycle State;
- When the LifecycleRegistry object accepts handlelevelcycleevent, it will trigger the onStateChanged method of the added LifecycleEventObserver; Then the adapters (fulllifecycle observer adapter, singlegenerated adapter observer, compositegenerated adapters observer, reflective generic lifecycle observer) notify the lifecycle observer.
The LifeCycle process in the fragment is the same as the Activity except that the monitoring method of the LifeCycle is different. Fragment is processed in its own LifeCycle method callback.
example
Define a lifecycle observer and define lifecycle events using the annotation onllifecycle event:
open class BizXXX : LifecycleObserver { @OnLifecycleEvent(value = Lifecycle.Event.ON_CREATE) fun onCreate() { } @OnLifecycleEvent(value = Lifecycle.Event.ON_START) fun onStart() { } @OnLifecycleEvent(value = Lifecycle.Event.ON_RESUME) fun onResume() { } @OnLifecycleEvent(value = Lifecycle.Event.ON_PAUSE) fun onPause() { } @OnLifecycleEvent(value = Lifecycle.Event.ON_STOP) fun onStop() { } @OnLifecycleEvent(value = Lifecycle.Event.ON_DESTROY) fun onDestroy() { } }
Or use DefaultLifecycleObserver to observe the lifecycle:
open class BizXXX : DefaultLifecycleObserver { override fun onCreate(owner: LifecycleOwner) { } override fun onStart(owner: LifecycleOwner) { } override fun onResume(owner: LifecycleOwner) { } override fun onPause(owner: LifecycleOwner) { } override fun onStop(owner: LifecycleOwner) { } override fun onDestroy(owner: LifecycleOwner) { } }
Add in Activity:
class YourActivity : BaseAppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) this.lifecycle.addObserver(BizXXX()) } }
Add in Fragment:
class YourFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) this.lifecycle.addObserver(BizXXX()) } }
When you can't use LifeCycle directly, you can also use LifeCycle indirectly. For example, the data layer should also monitor the changes of the life cycle, so you can define the Presenter layer as a LifeOwner:
open class BizPresenter : LifecycleObserver, LifecycleOwner { private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this) override fun getLifecycle(): Lifecycle { return lifecycleRegistry } @OnLifecycleEvent(value = Lifecycle.Event.ON_CREATE) fun onCreate() { lifecycle.addObserver(BizModel()) lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) } @OnLifecycleEvent(value = Lifecycle.Event.ON_START) fun onStart() { lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START) } @OnLifecycleEvent(value = Lifecycle.Event.ON_RESUME) fun onResume() { lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME) } @OnLifecycleEvent(value = Lifecycle.Event.ON_PAUSE) fun onPause() { lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE) } @OnLifecycleEvent(value = Lifecycle.Event.ON_STOP) fun onStop() { lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP) } @OnLifecycleEvent(value = Lifecycle.Event.ON_DESTROY) fun onDestroy() { lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) } }
class BizModel : LifecycleObserver { @OnLifecycleEvent(value = Lifecycle.Event.ON_CREATE) fun onCreate() { } @OnLifecycleEvent(value = Lifecycle.Event.ON_DESTROY) fun onDestroy() { } }
This nesting is also very practical.