Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
683cb282
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
683cb282
编写于
5月 28, 2022
作者:
D
dingyao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修正生命周期监听js样例
Signed-off-by:
N
dingyao
<
dingyao5@huawei.com
>
上级
f6aef3ec
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
43 addition
and
13 deletion
+43
-13
zh-cn/application-dev/reference/apis/js-apis-application-applicationContext.md
.../reference/apis/js-apis-application-applicationContext.md
+43
-13
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-application-applicationContext.md
浏览文件 @
683cb282
...
...
@@ -36,14 +36,6 @@ registerAbilityLifecycleCallback(callback: AbilityLifecycleCallback): **number**
| ------ | ------------------------------ |
| number | 返回的此次注册监听生命周期的id(每次注册该id会自增+1,当超过监听上限数量2^63-1时,返回-1)|
**示例:**
```
js
let
applicationContext
=
this
.
context
.
getApplicationContext
();
console
.
log
(
"
stage applicationContext:
"
+
JSON
.
stringify
(
applicationContext
));
let
lifecycleid
=
applicationContext
.
registerAbilityLifecycleCallback
(
AbilityLifecycleCallback
);
console
.
log
(
"
registerAbilityLifecycleCallback number:
"
+
JSON
.
stringify
(
lifecycleid
));
```
## ApplicationContext.unregisterAbilityLifecycleCallback
...
...
@@ -63,9 +55,47 @@ unregisterAbilityLifecycleCallback(callbackId: **number**, callback: AsyncCallb
**示例:**
```
js
let
applicationContext
=
this
.
context
.
getApplicationContext
();
console
.
log
(
"
stage applicationContext:
"
+
JSON
.
stringify
(
applicationContext
));
applicationContext
.
unregisterAbilityLifecycleCallback
(
lifecycleid
,
(
error
,
data
)
=>
{
console
.
log
(
"
unregisterAbilityLifecycleCallback success, err:
"
+
JSON
.
stringify
(
error
));
});
import
AbilityStage
from
"
@ohos.application.AbilityStage
"
;
var
lifecycleid
;
export
default
class
MyAbilityStage
extends
AbilityStage
{
onCreate
()
{
console
.
log
(
"
MyAbilityStage onCreate
"
)
let
AbilityLifecycleCallback
=
{
onAbilityCreate
(
ability
){
console
.
log
(
"
AbilityLifecycleCallback onAbilityCreate ability:
"
+
JSON
.
stringify
(
ability
));
},
onAbilityWindowStageCreate
(
ability
){
console
.
log
(
"
AbilityLifecycleCallback onAbilityWindowStageCreate ability:
"
+
JSON
.
stringify
(
ability
));
},
onAbilityWindowStageDestroy
(
ability
){
console
.
log
(
"
AbilityLifecycleCallback onAbilityWindowStageDestroy ability:
"
+
JSON
.
stringify
(
ability
));
},
onAbilityDestroy
(
ability
){
console
.
log
(
"
AbilityLifecycleCallback onAbilityDestroy ability:
"
+
JSON
.
stringify
(
ability
));
},
onAbilityForeground
(
ability
){
console
.
log
(
"
AbilityLifecycleCallback onAbilityForeground ability:
"
+
JSON
.
stringify
(
ability
));
},
onAbilityBackground
(
ability
){
console
.
log
(
"
AbilityLifecycleCallback onAbilityBackground ability:
"
+
JSON
.
stringify
(
ability
));
},
onAbilityContinue
(
ability
){
console
.
log
(
"
AbilityLifecycleCallback onAbilityContinue ability:
"
+
JSON
.
stringify
(
ability
));
}
}
// 1.通过context属性获取applicationContext
let
applicationContext
=
this
.
context
.
getApplicationContext
();
// 2.通过applicationContext注册监听应用内生命周期
lifecycleid
=
applicationContext
.
registerAbilityLifecycleCallback
(
AbilityLifecycleCallback
);
console
.
log
(
"
registerAbilityLifecycleCallback number:
"
+
JSON
.
stringify
(
lifecycleid
));
}
onDestroy
()
{
let
applicationContext
=
this
.
context
.
getApplicationContext
();
applicationContext
.
unregisterAbilityLifecycleCallback
(
lifecycleid
,
(
error
,
data
)
=>
{
console
.
log
(
"
unregisterAbilityLifecycleCallback success, err:
"
+
JSON
.
stringify
(
error
));
});
}
}
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录