https://github.com/alibaba/flutter_boost
- Flutter Engine
- Dart VM
一个进程里面最多只会初始化一个Dart VM。然而一个进程可以有多个Flutter Engine,多个Engine实力共享共同一个Dart VM - BinaryMessenger flutter和native通信是依赖BinaryMessenger去传递消息的。而目前BinaryMessenger是有FlutterViewController(Activity)去实现的。
Flutter Engine复用
iOS实现
通过以下代码就可以将flutterEngine渲染的页面转移到新的viewController显示。
flutterEngine?.viewController
新建FlutterViewController使用传递flutterEngine的方法初始化
- (instancetype)initWithEngine:(FlutterEngine*)engine
nibName:(nullable NSString*)nibName
bundle:(nullable NSBundle*)nibBundle NS_DESIGNATED_INITIALIZER;
android实现
- FlutterView:Android 中用于展示 Flutter 页面的控件,一个 FlutterView 中可以展示多个 Flutter Widget,官方的注释是:"An Android View containing a Flutter app"。内部包含了 FlutterNativeView。
- FlutterNativeView:每个 FlutterView 中包含一个FlutterNativeView,该类的主要作用是 Android 与 Flutter 之间的通信,保持生命周期与 Activity 及 FlutterView 同步。内部包含了 DartExecutor。
- DartExecutor:根据名称我们可以了解,这个类就是 Dart VM 相关处理 Java 与 C/C++ 的调用。官方的注释是:"Configures, bootstraps, and starts executing Dart code"。
主要代码如下
Application
// Instantiate a FlutterEngine.
FlutterEngine flutterEngine = new FlutterEngine(this);
// Start executing Dart code to pre-warm the FlutterEngine.
flutterEngine.getDartExecutor().executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
);
// Cache the FlutterEngine to be used by FlutterActivity.
FlutterEngineCache
.getInstance()
.put(TuyaConstant.flutterEngineId, flutterEngine);
MainActivity
override fun getCachedEngineId(): String? {
return TuyaConstant.flutterEngineId;
}
跳转到新的FlutterActivity
Intent intent = FlutterActivity
.withCachedEngine(TuyaConstant.flutterEngineId)
.build(getCurrentActivity());
getCurrentActivity().startActivity(intent);