UIAbility的生命周期包括Create、Foreground、Background、Destroy四個(gè)狀態(tài)
-
Create狀態(tài),在UIAbility實(shí)例創(chuàng)建時(shí)觸發(fā),對(duì)應(yīng)onCreate回調(diào)。可以在onCreate回調(diào)中進(jìn)行相關(guān)初始化操作
import UIAbility from '@ohos.app.ability.UIAbility'; import window from '@ohos.window'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { // 應(yīng)用初始化 // ... } // ... }
-
Foreground狀態(tài),在UIAbility切換至前臺(tái)時(shí)觸發(fā)。對(duì)應(yīng)onForeground回調(diào),在UIAbility的UI頁(yè)面可見(jiàn)之前,即UIAbility切換至前臺(tái)時(shí)觸發(fā)??梢栽趏nForeground回調(diào)中申請(qǐng)系統(tǒng)需要的資源,或者重新申請(qǐng)?jiān)趏nBackground中釋放的資源
import UIAbility from '@ohos.app.ability.UIAbility'; import window from '@ohos.window'; export default class EntryAbility extends UIAbility { // ... onForeground() { // 申請(qǐng)系統(tǒng)需要的資源,或者重新申請(qǐng)?jiān)趏nBackground中釋放的資源 // ... } }
-
Background狀態(tài),在UIAbility切換至后臺(tái)時(shí)觸發(fā)。對(duì)應(yīng)onBackground回調(diào),在UIAbility的UI頁(yè)面完全不可見(jiàn)之后,即UIAbility切換至后臺(tái)時(shí)候觸發(fā)??梢栽趏nBackground回調(diào)中釋放UI頁(yè)面不可見(jiàn)時(shí)無(wú)用的資源,或者在此回調(diào)中執(zhí)行較為耗時(shí)的操作,例如狀態(tài)保存等
import UIAbility from '@ohos.app.ability.UIAbility'; import window from '@ohos.window'; export default class EntryAbility extends UIAbility { // ... onBackground() { // 釋放UI頁(yè)面不可見(jiàn)時(shí)無(wú)用的資源,或者在此回調(diào)中執(zhí)行較為耗時(shí)的操作 // 例如狀態(tài)保存等 // ... } }
- ?Destroy狀態(tài),在UIAbility銷毀時(shí)觸發(fā)??梢栽趏nDestroy回調(diào)中進(jìn)行系統(tǒng)資源的釋放、數(shù)據(jù)的保存等操作
import UIAbility from '@ohos.app.ability.UIAbility'; import window from '@ohos.window'; export default class EntryAbility extends UIAbility { // ... onDestroy() { // 系統(tǒng)資源的釋放、數(shù)據(jù)的保存等 // ... } }
WindowStageCreate和WindowStageDestroy為窗口管理器(WindowStage)在UIAbility中管理UI界面功能的兩個(gè)生命周期回調(diào)
-
WindowStageCreate回調(diào),UIAbility實(shí)例創(chuàng)建完成之后,在進(jìn)入Foreground之前,系統(tǒng)會(huì)創(chuàng)建一個(gè)WindowStage。每一個(gè)UIAbility實(shí)例都對(duì)應(yīng)持有一個(gè)WindowStage實(shí)例。
WindowStage為本地窗口管理器,用于管理窗口相關(guān)的內(nèi)容,例如與界面相關(guān)的獲焦/失焦、可見(jiàn)/不可見(jiàn)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-793152.html可以在onWindowStageCreate回調(diào)中,設(shè)置UI頁(yè)面加載、設(shè)置WindowStage的事件訂閱。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-793152.html
import UIAbility from '@ohos.app.ability.UIAbility'; import window from '@ohos.window'; export default class EntryAbility extends UIAbility { // ... onWindowStageCreate(windowStage: window.WindowStage) { // 設(shè)置UI頁(yè)面加載 // 設(shè)置WindowStage的事件訂閱(獲焦/失焦、可見(jiàn)/不可見(jiàn)) // ... windowStage.loadContent('pages/Index', (err, data) => { // ... }); } // ... }
- WindowStageDestroy回調(diào),在UIAbility實(shí)例銷毀之前,會(huì)先進(jìn)入onWindowStageDestroy回調(diào),我們可以在該回調(diào)中釋放UI頁(yè)面資源。
?import UIAbility from '@ohos.app.ability.UIAbility'; import window from '@ohos.window'; export default class EntryAbility extends UIAbility { // ... onWindowStageDestroy() { // 釋放UI頁(yè)面資源 // ... } }
到了這里,關(guān)于HarmonyOS Stage模型 UIAbility生命周期狀態(tài)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!