public class Lifecycle extends Object
Ability or AbilitySlice.
Ability and AbilitySlice have their own lifecycle and dispatch their lifecycle states to
Lifecycle for management. You can call the Lifecycle#getLifecycleState() method to obtain their
current lifecycle state or call Lifecycle#addObserver(ILifecycleObserver) to add a lifecycle observer to
observe lifecycle changes in real-time. This class allows you to implement lifecycle callbacks of Ability
and AbilitySlice using methods from other classes. In this way, Ability and AbilitySlice
can take only their own responsibilities, enabling a decoupled application architecture.
You can call Ability#getLifecycle() and AbilitySlice#getLifecycle() to obtain Lifecycle
objects.
Ability.getLifecycle(),
AbilitySlice.getLifecycle(),
ILifecycleObserverSystemCapability.Aafwk#ABILITY| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
Lifecycle.Event |
Indicates a lifecycle event.
|
| 构造器 | 说明 |
|---|---|
Lifecycle() |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
addObserver(ILifecycleObserver observer) |
Adds a lifecycle observer.
|
void |
dispatchLifecycle(Lifecycle.Event event,
Intent intent) |
While Ability or AbilitySlice's lifecycle changes, dispatch lifecycle event.
|
Lifecycle.Event |
getLifecycleState() |
Obtains the current lifecycle event.
|
void |
removeObserver(ILifecycleObserver observer) |
Removes a lifecycle observer.
|
public void dispatchLifecycle(Lifecycle.Event event, Intent intent)
event - Lifecycle state.public void addObserver(ILifecycleObserver observer)
The observer will be notified of lifecycle changes.
observer - Indicates the lifecycle observer, either LifecycleObserver or
LifecycleStateObserver. The value cannot be null.LifecycleObserver,
LifecycleStateObserverpublic void removeObserver(ILifecycleObserver observer)
You are advised to call this method if you no longer need to listen to lifecycle changes. This reduces the performance loss caused by observing lifecycle changes.
observer - Indicates the lifecycle observer, either LifecycleObserver or
LifecycleStateObserver. The value cannot be null.LifecycleObserver,
LifecycleStateObserverpublic Lifecycle.Event getLifecycleState()
Lifecycle events drive lifecycle state changes. Therefore, you are able to know the lifecycle state once you
obtain the lifecycle event. For example, if the ON_ACTIVE event is received, the ability or ability
slice is in the ACTIVE state; if the ON_FOREGROUND event is received, the ability or ability
slice is changing from the BACKGROUND state to INACTIVE.