ILifecycleObserverpublic class LifecycleObserver extends Object implements ILifecycleObserver
Lifecycle dispatches a lifecycle event to a registered LifecycleObserver.
LifecycleObserver then implements lifecycle callbacks for Ability or AbilitySlice.
This allows your application to have a decoupled architecture where Ability or AbilitySlice takes
only a single responsibility.
You should inherit the LifecycleObserver class, override lifecycle callback methods as required, and
call Lifecycle#addObserver(ILifecycleObserver) to register a LifecycleObserver with
Lifecycle. The callback methods in LifecycleObserver provide the same functions as those provided
in Ability and AbilitySlice, but they are called in different sequence. The system calls those in
Ability and AbilitySlice first and then those in LifecycleObserver.
You are advised to use LifecycleObserver for lifecycle callbacks of Ability and
AbilitySlice when your application is complicated.
Lifecycle,
ILifecycleObserver,
LifecycleStateObserverSystemCapability.Aafwk#ABILITY| 构造器 | 说明 |
|---|---|
LifecycleObserver() |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
onActive() |
Called back in response to an
ON_ACTIVE event. |
void |
onBackground() |
Called back in response to an
ON_BACKGROUND event. |
void |
onForeground(Intent intent) |
Called back in response to an
ON_FOREGROUND event, where information for the ability or ability slice
to go back to the ACTIVE state is carried in the intent parameter. |
void |
onInactive() |
Called back in response to an
ON_INACTIVE event. |
void |
onStart(Intent intent) |
Called back in response to an
ON_START event, where the startup information is carried in the
intent parameter. |
void |
onStop() |
Called back in response to an
ON_STOP event. |
public void onStart(Intent intent)
ON_START event, where the startup information is carried in the
intent parameter.
This method initializes an Ability or AbilitySlice and is called back only once during the
entire lifecycle.
You are advised to implement some initialization logic using this method, for example, you can initialize a
timer or define some global objects.
intent - Indicates the startup information.Ability.onStart(Intent),
AbilitySlice.onStart(Intent)public void onInactive()
ON_INACTIVE event.
When an ON_INACTIVE event is received, the ability or ability slice is in the INACTIVE
state. INACTIVE is an intermediate state before the state changes to ACTIVE or
BACKGROUND. In this state, the UI may be visible but is not interactive.
You are advised not to use this method to invoke complex service logic.
Ability.onInactive(),
AbilitySlice.onInactive()public void onActive()
ON_ACTIVE event.
When an ON_ACTIVE event is received, the ability or ability slice is in the foreground and is
interactive.
Ability.onActive(),
AbilitySlice.onActive()public void onBackground()
ON_BACKGROUND event.
When an ON_BACKGROUND event is received, the ability or ability slice is invisible. You are advised
to suspend the threads related to this ability or ability slice and clear resources for more system memory.
Ability.onBackground(),
AbilitySlice.onBackground()public void onForeground(Intent intent)
ON_FOREGROUND event, where information for the ability or ability slice
to go back to the ACTIVE state is carried in the intent parameter.
When an ON_FOREGROUND event is received, the ability or ability slice returns to the foreground.
You can use this method to implement re-initialization or adjust the UI display by using the intent
parameter.
intent - Indicates the information for the ability or ability slice to go back to the
ACTIVE state.Ability.onForeground(Intent),
AbilitySlice.onForeground(Intent)public void onStop()
ON_STOP event.
This method is called back when the lifecycle of the ability or ability slice is destroyed. You can reclaim resources using this method.
Ability.onStop(),
AbilitySlice.onStop()