首页 > 其他分享 >harmony_flutter_FlutterEngineGroup

harmony_flutter_FlutterEngineGroup

时间:2024-12-18 09:23:18浏览次数:10  
标签:engine getInstance FlutterEngineGroup EntryAbility private void harmony context 

harmony_flutter_FlutterEngineGroup介绍

1. EntryAbility 修改为继承自 UIAbility

export default class EntryAbility extends UIAbility implements ExclusiveAppComponent<UIAbility>{
  detachFromFlutterEngine(): void {
    // throw new Error('Method not implemented.');
  }

  getAppComponent(): UIAbility {
    return this;
  }

  static app?: EntryAbility;

  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    FlutterManager.getInstance().pushUIAbility(this);
    EntryAbility.app = this;
  }

  onDestroy(): void | Promise<void> {
    FlutterManager.getInstance().popUIAbility(this);
    EntryAbility.app = undefined;
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    FlutterManager.getInstance().pushWindowStage(this, windowStage);
    windowStage.loadContent('pages/MainPage');
  }

  onWindowStageDestroy() {
    FlutterManager.getInstance().popWindowStage(this);
  }
}

2. 封装 FlutterEngine 的 attach 和 detach 操作

export class EngineBindings implements DataModelObserver {
  private engine?: FlutterEngine;
  private channel?: MethodChannel;
  private context: common.Context;
  private delegate: EngineBindingsDelegate;
  private flutterView: FlutterView;

  constructor(context: common.Context, delegate: EngineBindingsDelegate) {
    this.context = context;
    this.delegate = delegate;
    this.flutterView = FlutterManager.getInstance().createFlutterView(context);
  }

  getFlutterViewId() {
    return this.flutterView.getId();
  }

  async attach() {
    if (this.engine) {
      Log.i("Multi->attach", "engine is ");
      return;
    }
    DataModel.instance.addObserver(this);
    // 建议按以下代码顺序,依次执行 1、2、3、4、5
    // 1.
    await engines.checkLoader(this.context, []);
    let options: Options = new Options(this.context).setDartEntrypoint(DartEntrypoint.createDefault());
    // 2. 
    this.engine = await engines.createAndRunEngineByOptions(options) ?? undefined;
    if (!this.engine) {
      throw new Error("Create engine failed.");
    }
    // 3.
    this.engine.getLifecycleChannel()?.appIsResumed();
    if (EntryAbility.app) {
      // 4.
      this.engine.getAbilityControlSurface()?.attachToAbility(EntryAbility.app);
    }
    // 5.
    this.flutterView.attachToFlutterEngine(this.engine);
    GeneratedPluginRegistrant.registerWith(this.engine);

    // ...
  }

  detach() {
    this.flutterView.detachFromFlutterEngine();
    this.engine?.destroy();
    DataModel.instance.removeObserver(this);
    this.channel?.setMethodCallHandler(null);
  }

  // ...
}

3. 调用 FlutterEngine 的 attach 和 detach 操作

@Entry()
@Component
struct SingleFlutterPage {
  @State viewId: string = "";
  private context = getContext(this) as common.UIAbilityContext
  private engineBindings: EngineBindings = new EngineBindings(this.context, this);

  onNext() {
    router.pushUrl({ "url": "pages/MainPage" });
  }

  aboutToAppear() {
    Log.i("Multi->aboutToAppear", "SingleFlutterPage");
    this.viewId = this.engineBindings.getFlutterViewId();
    Log.i("Multi->aboutToAppear", "SingleFlutterPage, id=" + this.viewId);
    this.engineBindings.attach();
  }

  aboutToDisappear(): void {
    this.engineBindings.detach();
  }

  build() {
    Column() {
      FlutterPage({ viewId: this.viewId, xComponentType: XComponentType.TEXTURE }).backgroundColor(Color.Transparent)
    }
  }
}

标签:engine,getInstance,FlutterEngineGroup,EntryAbility,private,void,harmony,context,
From: https://www.cnblogs.com/hongmengos/p/18613866

相关文章

  • flutter_ohos_5
    ohos开发鸿蒙(基于5.0.0版本)原始仓来源:https://github.com/flutter/flutter1.仓库说明本仓库是基于FlutterSDK对于OpenHarmony平台的兼容拓展,可支持IDE或者终端使用FlutterTools指令编译和构建OpenHarmony应用程序。2.环境依赖说明:1.FlutterTools指令目前已支持在Linux......
  • Flutter OHOS harmony_fluwx 集成微信服务(二)
    harmony_fluwx集成微信服务(2)fluwx链接:https://gitee.com/almost777/fluwx接入功能分享图片,文本,音乐,视频等。支持分享到会话,朋友圈以及收藏.微信支付.在微信登录时,获取AuthCode.拉起小程序.订阅消息.打开微信.从微信标签打开应用初始化注册WxAPIregisterWxApi(ap......
  • Flutter OHOS flutter_dart_native
    DartNativeDartNative作为Dart和原生API之间沟通的桥梁。用更快、更简洁的代码替换性能低下的Flutter通道。特征动态同步和异步通道DartNative动态调用任何原生API。它支持同步和异步通道。多语言接口直接调用不再需要像FlutterChannel那样对参数和返回值进行......
  • Flutter OHOS flutter appscheme插件
    FlutterAppScheme配置说明1、Android端配置说明在您项目中Android的AndroidManifest.xml文件中按照如下规范添加Scheme,例如android/app/src/main/AndroidManifest.xmla、在需要启动的Activity中新增以下格式的代码<!--AndroidScheme--><intent-filter><actionandro......
  • 四大跨平台开发框架深度解析——uniapp、uniapp-X、React Native与Flutter
    引言随着移动互联网的飞速发展,跨平台开发框架成为了开发者们关注的焦点。这些框架旨在通过编写一套代码,实现多个平台的应用开发,从而大幅提高开发效率和降低维护成本。本文将深入剖析uniapp、uniapp-X、ReactNative和Flutter这四个主流的跨平台开发框架,探讨它们的优缺点及......
  • 「九」HarmonyOS 5 端云一体化实战项目——「M.U.」应用云侧开发云数据库
    1立意背景M.代表“我”,U.代表“你”,这是一款用于记录情侣从相识、相知、相恋、见家长、订婚直至结婚等各个阶段美好记忆留存的应用程序。它旨在为情侣们提供一个专属的空间,让他们能够将一路走来的点点滴滴,如初次相遇时的心动瞬间、逐渐了解彼此过程中的深入交流、甜蜜......
  • HarmonyOS应用开发--LazyForeach懒加载
    引言在鸿蒙应用开发中,ArkTS(ArkTypeScript)作为开发语言,提供了丰富的API来构建高效、流畅的应用程序。其中,LazyForeach作为一种懒加载技术,对于优化性能和提升用户体验具有重要作用。本文将对LazyForeach懒加载进行深入学习总结。ArkTS简介ArkTS是基于TypeScript的开发语言,专......
  • OpenHarmony测试RS232/RS485串口方法,触觉智能SBC3528工控主板演示
    教大家介绍在OpenHarmony系统,没有串口工具的情况下如何测试RS232/RS485,使用触觉智能SBC3528工控主板演示,搭载了瑞芯微RK3568四核处理器,板载2路RS232+4路隔离RS485,集成DIDO,自研RS485自动收发驱动,支持超2KM传输距离!RS485测试方法以触觉智能SBC3528工控主板为例,如果需要测试RS485串......
  • HarmonyOS 获取进程相关的信息process 常用的几个方法
    获取进程相关的信息,提供进程管理的相关功能。process1.EventListener2.isIsolatedProcess3.is64Bit4.getStartRealtime5.getPastCpuTime导入模块import{process}from'@kit.ArkTS';属性名称类型可读可写说明uidnumber是否进程的用户标识。pidnumber是......
  • HarmonyOS:@Extend装饰器:定义扩展组件样式
    一、前言使用@Styles用于样式的重用,在@Styles的基础上,使用@Extend扩展原生组件样式。说明从APIversion9开始,该装饰器支持在ArkTS卡片中使用。从APIversion11开始,该装饰器支持在元服务中使用。二、装饰器使用说明语法@Extend(UIComponentName)functionfu......