diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets index 1f3c735b0ef795858c91cc0ecf71c058ff421124..4a0c5e3e803b99ddaf4710226569186c1c10d621 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets @@ -39,7 +39,6 @@ import Any from '../../plugin/common/Any'; const TAG = "FlutterAbilityDelegate"; const PLUGINS_RESTORATION_BUNDLE_KEY = "plugins"; const FRAMEWORK_RESTORATION_BUNDLE_KEY = "framework"; -const EVENT_BACK_PRESS = 'EVENT_BACK_PRESS'; /** * 主要职责: @@ -92,14 +91,6 @@ class FlutterAbilityAndEntryDelegate implements ExclusiveAppComponent this.flutterView.attachToFlutterEngine(this.flutterEngine!!); } this.host?.configureFlutterEngine(this.flutterEngine!!); - this.context.eventHub.on(EVENT_BACK_PRESS, () => { - if (this.flutterView?.getKeyboardHeight() == 0) { - this.flutterEngine?.getNavigationChannel()?.popRoute(); - this.flutterEngine?.getPlatformViewsController()?.setBackNodeControllers(); - } else { - this.flutterEngine?.getTextInputChannel()?.textInputMethodHandler?.hide(); - } - }); } /** @@ -307,7 +298,6 @@ class FlutterAbilityAndEntryDelegate implements ExclusiveAppComponent } this.isAttached = false; - this.context?.eventHub.off(EVENT_BACK_PRESS); } onLowMemory(): void { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index f402b642b7c1b0865a801b17821a236a3b2a6312..320f9923b2f59828a48ee74a9e723a504831128f 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -30,6 +30,7 @@ import { accessibility } from '@kit.AccessibilityKit'; import TextInputPlugin from '../plugin/editing/TextInputPlugin'; const TAG = "FlutterViewTag"; +const EVENT_BACK_PRESS = 'EVENT_BACK_PRESS'; export class ViewportMetrics { devicePixelRatio: number = 1.0; @@ -184,6 +185,7 @@ export class FlutterView { private mouseCursorPlugin?: MouseCursorPlugin; private textInputPlugin?: TextInputPlugin; private uiContext?: UIContext | undefined; + private context: Context; private settings?: Settings; private mFirstFrameListeners: ArrayList; private isFlutterUiDisplayed: boolean = false; @@ -199,7 +201,8 @@ export class FlutterView { private keyboardAvoidArea: window.AvoidArea; constructor(viewId: string, context: Context) { - this.id = viewId + this.id = viewId; + this.context = context; this.displayInfo = display.getDefaultDisplaySync(); this.viewportMetrics.devicePixelRatio = this.displayInfo?.densityPixels; @@ -433,6 +436,15 @@ export class FlutterView { } this.onAreaChange(newArea, true); + this.context.eventHub.on(EVENT_BACK_PRESS, () => { + if (this?.getKeyboardHeight() == 0) { + this.flutterEngine?.getNavigationChannel()?.popRoute(); + this.flutterEngine?.getPlatformViewsController()?.setBackNodeControllers(); + } else { + this.flutterEngine?.getTextInputChannel()?.textInputMethodHandler?.hide(); + } + }); + let windowId = this.mainWindow?.getWindowProperties()?.id ?? 0 this.mouseCursorPlugin = new MouseCursorPlugin(windowId, this.flutterEngine?.getMouseCursorChannel()!); this.textInputPlugin = new TextInputPlugin(this.flutterEngine?.getTextInputChannel()!); @@ -459,6 +471,7 @@ export class FlutterView { this.flutterEngine = null; this.keyboardManager = null; this.textInputPlugin?.destroy(); + this.context?.eventHub.off(EVENT_BACK_PRESS); } onWindowCreated() {