delegate = new FlutterActivityAndFragmentDelegate(this);
delegate.onAttach(this);
…
setContentView(createFlutterView());
我们一步一步来,首先创建了FlutterActivityAndFragmentDelegate 并调用了它的attact(this)方法。
FlutterActivityAndFragmentDelegate
void onAttach(@NonNull Context context) {
ensureAlive();
///初始化engine
if (flutterEngine == null) {
///这里面会对已有的engine进行复用
setupFlutterEngine();
}
///初始化平台插件
///本质上,是将engine的 channel回调与平台的系统服务进行绑定
///如:震动、复制粘贴、声音播放等…
platformPlugin = host.providePlatformPlugin(host.getActivity(), flutterEngine);
if (host.shouldAttachEngineToActivity()) {
Log.v(TAG, “Attaching FlutterEngine to the Activity that owns this Fragment.”);
/// 激活 原生viewController
/// 并通知相关插件
/// PlatformViewsController 这个类你应该很熟悉(如果你接入过原生view的话)
flutterEngine
.getActivityControlSurface()
.attachToActivity(host.getActivity(), host.getLifecycle());
}
///注册插件
///通过反射调用 “io.flutter.plugins.GeneratedPluginRegistrant”
///的 “registerWith”方法,这个过程走完了,你的插件基本就能用了
host.configureFlutterEngine(flutterEngine);
}
通过上面,我们大致了解了,在flutter端使用的平台功能是什么时候装配的了。
我们回到FlutterActivity,继续重要的第二步:
setContentView(createFlutterView());
@NonNull
private View createFlutterView() {
return delegate.onCreateView(
null /* inflater /, null / container /, null / savedInstanceState */);
}
最终会调用 del