6

我在我的 Android 应用程序中使用了 Flutter 模块。我在我的 Activity 中定义了一个处于预热状态的全局 Flutter Engine,并在特定场景中需要时将其附加到 FlutterFragment。下面是触发 onCreate()on Activity 后调用的预热引擎。

void setupFlutterEngine(){
        FlutterEngine flutterEngine=new FlutterEngine(getApplicationContext());

//        Start executing Dart code in the FlutterEngine.
        flutterEngine.getDartExecutor().executeDartEntrypoint(
                DartExecutor.DartEntrypoint.createDefault()
        );

//      Cache the pre-warmed FlutterEngine to be used later by FlutterFragment.
        FlutterEngineCache
                .getInstance()
                .put(FLUTTER_ENGINE_CACHE_TAG, flutterEngine);

    }

一切正常,我正在实例化缓存引擎并将其添加到我的 FlutterFragment 中,如下所示:

FlutterEngine flutterEngine=FlutterEngineCache.getInstance().get(FLUTTER_ENGINE_CACHE_TAG);

flutterMethodChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), methodChannelTag);

flutterMethodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                
            }
        });

        GeneratedPluginRegistrant.registerWith(flutterEngine); 

flutterFragment = FlutterFragment
                .withCachedEngine(FLUTTER_ENGINE_CACHE_TAG)
                .renderMode(RenderMode.surface)
                .transparencyMode(TransparencyMode.opaque)
                .shouldAttachEngineToActivity(true)
                .build();

我想在片段被破坏但不想破坏引擎时完成flutter模块的当前视图,我想重用FlutterEngine并希望从Flutter模块中main()方法下的第一个屏幕重新开始当再次添加片段时。

4

0 回答 0