首页 > 其他分享 >UIAbility组件生命周期

UIAbility组件生命周期

时间:2024-10-18 14:01:08浏览次数:1  
标签:生命周期 windowStage UIAbility window 组件 import 回调 WindowStage

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(MaoistLearning)
➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ 
➤GitHub地址:https://github.com/strengthen
➤原文地址:https://www.cnblogs.com/strengthen/p/18474132
➤如果链接不是为敢技术的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

 

概述

当用户打开、切换和返回到对应应用时,应用中的UIAbility实例会在其生命周期的不同状态之间转换。UIAbility类提供了一系列回调,通过这些回调可以知道当前UIAbility实例的某个状态发生改变,会经过UIAbility实例的创建和销毁,或者UIAbility实例发生了前后台的状态切换。

UIAbility的生命周期包括Create、Foreground、Background、Destroy四个状态,如下图所示。

图1 UIAbility生命周期状态

 

生命周期状态说明

 

Create状态

Create状态为在应用加载过程中,UIAbility实例创建完成时触发,系统会调用onCreate()回调。可以在该回调中进行页面初始化操作,例如变量定义资源加载等,用于后续的UI展示。

   
  1. import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
  2. export default class EntryAbility extends UIAbility {
  3. onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  4. // 页面初始化
  5. }
  6. // ...
  7. }
说明

Want是对象间信息传递的载体,可以用于应用组件间的信息传递。Want的详细介绍请参见信息传递载体Want

 

WindowStageCreate和WindowStageDestroy状态

UIAbility实例创建完成之后,在进入Foreground之前,系统会创建一个WindowStage。WindowStage创建完成后会进入onWindowStageCreate()回调,可以在该回调中设置UI加载、设置WindowStage的事件订阅。

图2 WindowStageCreate和WindowStageDestroy状态

在onWindowStageCreate()回调中通过loadContent()方法设置应用要加载的页面,并根据需要调用on('windowStageEvent')方法订阅WindowStage的事件(获焦/失焦、可见/不可见)。

   
  1. import { UIAbility } from '@kit.AbilityKit';
  2. import { window } from '@kit.ArkUI';
  3. import { hilog } from '@kit.PerformanceAnalysisKit';
  4. const TAG: string = '[EntryAbility]';
  5. const DOMAIN_NUMBER: number = 0xFF00;
  6. export default class EntryAbility extends UIAbility {
  7. // ...
  8. onWindowStageCreate(windowStage: window.WindowStage): void {
  9. // 设置WindowStage的事件订阅(获焦/失焦、可见/不可见)
  10. try {
  11. windowStage.on('windowStageEvent', (data) => {
  12. let stageEventType: window.WindowStageEventType = data;
  13. switch (stageEventType) {
  14. case window.WindowStageEventType.SHOWN: // 切到前台
  15. hilog.info(DOMAIN_NUMBER, TAG, 'windowStage foreground.');
  16. break;
  17. case window.WindowStageEventType.ACTIVE: // 获焦状态
  18. hilog.info(DOMAIN_NUMBER, TAG, 'windowStage active.');
  19. break;
  20. case window.WindowStageEventType.INACTIVE: // 失焦状态
  21. hilog.info(DOMAIN_NUMBER, TAG, 'windowStage inactive.');
  22. break;
  23. case window.WindowStageEventType.HIDDEN: // 切到后台
  24. hilog.info(DOMAIN_NUMBER, TAG, 'windowStage background.');
  25. break;
  26. default:
  27. break;
  28. }
  29. });
  30. } catch (exception) {
  31. hilog.error(DOMAIN_NUMBER, TAG, 'Failed to enable the listener for window stage event changes. Cause:' + JSON.stringify(exception));
  32. }
  33. hilog.info(DOMAIN_NUMBER, TAG, '%{public}s', 'Ability onWindowStageCreate');
  34. // 设置UI加载
  35. windowStage.loadContent('pages/Index', (err, data) => {
  36. // ...
  37. });
  38. }
  39. }
说明

WindowStage的相关使用请参见窗口开发指导

对应于onWindowStageCreate()回调。在UIAbility实例销毁之前,则会先进入onWindowStageDestroy()回调,可以在该回调中释放UI资源。

   
  1. import { UIAbility } from '@kit.AbilityKit';
  2. import { window } from '@kit.ArkUI';
  3. import { hilog } from '@kit.PerformanceAnalysisKit';
  4. import { BusinessError } from '@kit.BasicServicesKit';
  5. const TAG: string = '[EntryAbility]';
  6. const DOMAIN_NUMBER: number = 0xFF00;
  7. export default class EntryAbility extends UIAbility {
  8. windowStage: window.WindowStage | undefined = undefined;
  9. // ...
  10. onWindowStageCreate(windowStage: window.WindowStage): void {
  11. this.windowStage = windowStage;
  12. // ...
  13. }
  14. onWindowStageDestroy() {
  15. // 释放UI资源
  16. // 例如在onWindowStageDestroy()中注销获焦/失焦等WindowStage事件
  17. try {
  18. if (this.windowStage) {
  19. this.windowStage.off('windowStageEvent');
  20. }
  21. } catch (err) {
  22. let code = (err as BusinessError).code;
  23. let message = (err as BusinessError).message;
  24. hilog.error(DOMAIN_NUMBER, TAG, `Failed to disable the listener for windowStageEvent. Code is ${code}, message is ${message}`);
  25. }
  26. }
  27. }

 

WindowStageWillDestroy状态

对应onWindowStageWillDestroy()回调,在WindowStage销毁前执行,此时WindowStage可以使用。

   
  1. import { UIAbility } from '@kit.AbilityKit';
  2. import { window } from '@kit.ArkUI';
  3. export default class EntryAbility extends UIAbility {
  4. windowStage: window.WindowStage | undefined = undefined;
  5. // ...
  6. onWindowStageCreate(windowStage: window.WindowStage): void {
  7. this.windowStage = windowStage;
  8. // ...
  9. }
  10. onWindowStageWillDestroy(windowStage: window.WindowStage) {
  11. // 释放通过windowStage对象获取的资源
  12. }
  13. onWindowStageDestroy() {
  14. // 释放UI资源
  15. }
  16. }
说明

WindowStage的相关使用请参见窗口开发指导

 

Foreground和Background状态

Foreground和Background状态分别在UIAbility实例切换至前台和切换至后台时触发,对应于onForeground()回调和onBackground()回调。

onForeground()回调,在UIAbility的UI可见之前,如UIAbility切换至前台时触发。可以在onForeground()回调中申请系统需要的资源,或者重新申请在onBackground()中释放的资源。

onBackground()回调,在UIAbility的UI完全不可见之后,如UIAbility切换至后台时候触发。可以在onBackground()回调中释放UI不可见时无用的资源,或者在此回调中执行较为耗时的操作,例如状态保存等。

例如应用在使用过程中需要使用用户定位时,假设应用已获得用户的定位权限授权。在UI显示之前,可以在onForeground()回调中开启定位功能,从而获取到当前的位置信息。

当应用切换到后台状态,可以在onBackground()回调中停止定位功能,以节省系统的资源消耗。

   
  1. import { UIAbility } from '@kit.AbilityKit';
  2. export default class EntryAbility extends UIAbility {
  3. // ...
  4. onForeground(): void {
  5. // 申请系统需要的资源,或者重新申请在onBackground()中释放的资源
  6. }
  7. onBackground(): void {
  8. // 释放UI不可见时无用的资源,或者在此回调中执行较为耗时的操作
  9. // 例如状态保存等
  10. }
  11. }

当应用的UIAbility实例已创建,且UIAbility配置为singleton启动模式时,再次调用startAbility()方法启动该UIAbility实例时,只会进入该UIAbility的onNewWant()回调,不会进入其onCreate()onWindowStageCreate()生命周期回调。应用可以在该回调中更新要加载的资源和数据等,用于后续的UI展示。

   
  1. import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
  2. export default class EntryAbility extends UIAbility {
  3. // ...
  4. onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) {
  5. // 更新资源、数据
  6. }
  7. }

 

Destroy状态

Destroy状态在UIAbility实例销毁时触发。可以在onDestroy()回调中进行系统资源的释放、数据的保存等操作。

例如,调用terminateSelf()方法停止当前UIAbility实例,执行onDestroy()回调,并完成UIAbility实例的销毁。

   
  1. import { UIAbility } from '@kit.AbilityKit';
  2. export default class EntryAbility extends UIAbility {
  3. // ...
  4. onDestroy() {
  5. // 系统资源的释放、数据的保存等
  6. }
  7. }

 

标签:生命周期,windowStage,UIAbility,window,组件,import,回调,WindowStage
From: https://www.cnblogs.com/strengthen/p/18474132

相关文章

  • UIAbility组件概述
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/......
  • UIAbility组件间交互(设备内)
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/......
  • UIAbility组件与UI的数据同步
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/......
  • UIAbility组件基本用法
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/......
  • UIAbility组件启动模式
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/......
  • Unity生命周期
    基础概念游戏的本质就是一个死循环,每一次循环都会处理游戏逻辑并更新一次游戏画面之所以能看到画面在动,是因为切换画面速度达到一定速度时人眼就会认为画面是动态且流畅的一帧就是执行了一次循环,Unity底层已经封装好了这个死循环我们只需要利用Unity的生命周期函数的规则来执行游......
  • UIAbility组件基础
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/......
  • Android Framework AMS(08)service组件分析-2(startService和StopService关键流程分析)
    该系列文章总纲链接:专题总纲目录AndroidFramework总纲本章关键点总结&说明:说明:上一章节主要解读应用层service组件启动的2种方式startService和bindService,以及从APP层到AMS调用之间的打通。本章节主要关注service组件启动方式的一种:startService启动方式,分析关键API......
  • Android Framework AMS(09)service组件分析-3(bindService和unbindService关键流程分析)
    该系列文章总纲链接:专题总纲目录AndroidFramework总纲本章关键点总结&说明:说明:上上一章节主要解读应用层service组件启动的2种方式startService和bindService,以及从APP层到AMS调用之间的打通。上一章节我们关注了service组件启动方式的一种:startService启动方式。本章......
  • [Spring]——IOC,DI,Bean的生命周期
    目录一.反转(转移)控制(IOC)二.DI依赖注入三.bean的生命周期1.对象的生命周期2.为什么要学习生命周期3.生命周期的三个阶段(1)创建(2)初始化方法两种实现方法细节问题(3)销毁(资源释放的操作)两种实现方法(和初始化的差不多)细节分析4.对象生命周期的总结完整代码5.后置处......