HarmonyOS 鸿蒙Next 多实例、多窗口的最佳实践demo
HarmonyOS 鸿蒙Next 多实例、多窗口的最佳实践demo 这个有最佳实践么?或者demo样例也可以;
我这面写了个简版demo,参考下
// 新建 ..../..../DocumentListPage.ets
import common from '@ohos.app.ability.common'
import { Want } from '@ohos.app.ability.Want'
import router from '@ohos.router';
@Entry
@Component
struct DocumentListPage {
private index: number = 1
@State docs: number[] = []
private context = this.getContext(this) as common.UIAbilityContext
build() {
Row() {
Column({ space: 10 }) {
Row({ space: 5 }){
Image('')
.backgroundColor(Color.Blue)
.size({ width: 30, height: 30 })
.onClick(() => {
router.back()
})
Text('文档列表')
.fontSize(28)
.fontWeight(FontWeight.Bold)
}
.padding({ left: 10, right: 10 })
.width('100%')
.backgroundColor(Color.Red)
.height(40)
Divider()
Button('新建文档')
.onClick(() => {
// 添加文档
this.docs.push(this.index)
// 跳转的目的地want
let want: Want = {
deviceId: '',
bundleName: 'com.example.xxx',
moduleName: 'entry',
abilityName: 'DocumentAbility',
parameters: {
instanceKey: 'idx_' + this.index++
}
}
// 跳转
this.context.startAbility(want)
})
ForEach(this.docs, (idx: number) => {
Row({ space: 10 }) {
Image('')
.width(20)
.aspectRatio(1)
.backgroundColor(Color.Red)
Text(`文档${idx}`)
.fontSize(18)
.onClick(() => {
// 跳转的目的地want
let want: Want = {
deviceId: '',
bundleName: 'com.example.yjbasicproject',
moduleName: 'entry',
abilityName: 'DocumentAbility',
parameters: {
instanceKey: 'idx_' + idx
}
}
// 跳转
this.context.startAbility(want)
})
}.width('100%')
})
}
.width('100%')
.height('100%')
.padding(20)
}
}
}
// 新建 ..../..../DocumentPage.ets
import common from '@ohos.app.ability.common'
import { Want } from '@ohos.app.ability.Want'
@Entry
@Component
struct DocumentPage {
private context = this.getContext(this) as common.UIAbilityContext
@State editTitle: boolean = true
@State title: string = '标题'
@State content: string = ''
build() {
Row() {
Column({ space: 10 }) {
Row({ space: 10 }) {
Image('')
.width(50)
.aspectRatio(1)
.backgroundColor(Color.Red)
.onClick(() => {
// 跳转的目的地want
let want: Want = {
deviceId: '',
bundleName: 'com.example.xxxxx',
moduleName: 'entry',
abilityName: 'EntryAbility',
}
// 跳转
this.context.startAbility(want)
})
if (!this.editTitle) {
Text(this.title)
.fontSize(30)
.fontWeight(FontWeight.Bold)
.onClick(() => this.editTitle = true)
} else {
TextInput({
placeholder: '请输入标题',
text: this.title
})
.layoutWeight(1)
.onChange(val => this.title = val)
Button('确定')
.onClick(() => this.editTitle = false)
}
}
.width('100%')
Divider()
TextArea({
placeholder: '请输入文档内容',
text: this.content
})
.layoutWeight(1)
.onChange(val => this.content = val)
}
.width('100%')
.height('100%')
.padding(10)
}
}
}
3、新建DocumentAbility
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
export default class DocumentAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
}
onDestroy(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Document/DocumentPage', (err) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// Ability has back to background
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
}
}
4、新建MyAbilityStage.ets
import { AbilityStage } from '@ohos.app.ability.AbilityStage';
import { Want } from '@ohos.app.ability.Want';
export default class MyAbilityStage extends AbilityStage {
onAcceptWant(want: Want): string {
if (want.abilityName === 'DocumentAbility') {
return `DocAbilityInstance_${want.parameters?.instanceKey}`
}
return ''
}
}
5、module.json5 配置文件
// 增加
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:layered_image",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
]
},
{
"name": "DocumentAbility",
"srcEntry": "./ets/documentability/DocumentAbility.ets",
"description": "$string:DocumentAbility_desc",
"icon": "$media:layered_image",
"label": "$string:DocumentAbility_label",
"launchType": "specified",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background"
},
],
// 增加
"srcEntry": "./ets/AbilityStage/MyAbilityStage.ets",
更多关于HarmonyOS 鸿蒙Next 多实例、多窗口的最佳实践demo的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
针对“HarmonyOS 鸿蒙Next 多实例、多窗口的最佳实践demo”的问题,以下是一个简洁的回答:
HarmonyOS 鸿蒙Next在多实例、多窗口方面提供了丰富的API和框架支持,以实现更高效、灵活的应用交互。为了展示最佳实践,开发者可以关注以下几个方面来构建demo:
-
多实例管理:利用HarmonyOS提供的生命周期管理API,实现应用实例的创建、销毁和状态恢复。确保在多实例场景下,应用能够正确响应系统事件并维护数据一致性。
-
多窗口布局:采用响应式布局技术,使应用能够在不同屏幕尺寸和分辨率下自适应显示。利用HarmonyOS的窗口管理API,实现窗口的创建、切换、隐藏和显示等功能。
-
数据共享与同步:在多实例、多窗口间实现数据的高效共享与同步,避免数据冲突和丢失。可以利用HarmonyOS提供的数据存储和通信机制,如全局变量、消息队列等。
-
性能优化:在多实例、多窗口场景下,应用性能可能受到影响。因此,需要对资源使用进行监控和优化,确保应用在多实例、多窗口下仍能流畅运行。
为了获取更具体的实现代码和示例,建议参考HarmonyOS官方文档和开发者社区的资源。如果问题依旧没法解决请联系官网客服,官网地址是: https://www.itying.com/category-93-b0.html。