android - 使用缓存引擎启动 FlutterActivity 子类

标签 android flutter kotlin

我目前正在将使用 Flutter 开发的 View 添加到现有的 Android 应用程序中。我一直在关注 Flutter website 中的教程。并决定使用缓存引擎,以最大程度地减少用户在导航到应用程序的 Flutter 部分时可能遇到的延迟。为此,您必须启动 Flutter Activity ,例如

startActivity(
    FlutterActivity
      .withCachedEngine("my_engine_id")
      .build(this) // this is a Context

)

过了一会儿,我需要编写一个方法 channel 来从应用程序的 Flutter 部分与 Android 主机应用程序进行通信,所以我按照 Flutter 的另一个 tutorials 中的说明进行操作。 ,其中显示实现 channel 的 Activity 必须扩展 FlutterActivity .

所以我的问题是我不确定如何使用缓存引擎初始化这个 Activity ,因为我显然不能使用 FlutterActivity.withCachedEngine了。有没有人已经解决了这个问题?

最佳答案

看完FlutterActivity我找到了 provideFlutterEngine 的文档method .文档描述清楚地指出:

This hook is where a cached FlutterEngine should be provided, if a cached FlutterEngine is desired.



所以我的类的最终实现现在看起来像这样
class MyActivity : FlutterActivity() {

    override fun provideFlutterEngine(context: Context): FlutterEngine? =
        FlutterEngineCache.getInstance().get(FlutterConstants.ENGINE_ID)

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "my-channel")
            .setMethodCallHandler { call, result ->
                if (call.method == "my-method") {
                    myMethod()
                    result.success(null)
                } else {
                    result.notImplemented()
                }
            }
    }

    private fun myMethod() {
        // Do native stuff
    }

}

我只是开始写startActivity(Intent(this, MyActivity::class.java))

关于android - 使用缓存引擎启动 FlutterActivity 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62297524/

相关文章:

android - 如何使列表元素在滚动后获得 DragEvents

Flutter断言失败: line 3105 pos 16: 'node.parent == null || !node.parent!.isPartOfNodeMerging || node.isMergedIntoParent' : is not true

flutter - 如何在 Windows 上以 HTML 格式查看 flutter/dart 的覆盖率报告

android - 为什么振动在 Android P (API 28) 上不起作用?

java - 在水平 ScrollView 中添加许多图像?

Android 下载管理器 'Download Requires Network' 错误

android - 在 android 通知中创建一个 map Activity

dart - 如何检查当前路线是哪条?

android - Android Volley有时会导致两次POST

android - Dagger2 服务中的 RuntimeException AndroidInjection.inject