@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
switchLaunchThemeForNormalTheme();
super.onCreate(savedInstanceState);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
delegate = new FlutterActivityAndFragmentDelegate(this);
///创建绑定引擎等
delegate.onAttach(this);
///用于插件、框架恢复状态
delegate.onActivityCreated(savedInstanceState);
///设置窗口背景透明,隐藏 status bar
configureWindowForTransparency();
///这里是咱们的入口
setContentView(createFlutterView());
configureStatusBarForFullscreenFlutterExperience();
}
setContentView大家很熟悉,我们直接看createFlutterView() 这个方法:
@NonNull
private View createFlutterView() {
return delegate.onCreateView(
null /* inflater /, null / container /, null / savedInstanceState */);
}
FlutterActivityAndFragmentDelegate
flutter的初始化、启动等操作都是委托给它的。
我们继续看onCreateView,我将说明以注释的形式写在代码里
@NonNull
View onCreateView(
LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.v(TAG, “Creating FlutterView.”);
ensureAlive();
if (host.getRenderMode() == RenderMode.surface) {
/// flutter 应用在surface上显示,所以会进入到这里
FlutterSurfaceView flutterSurfaceView =
new FlutterSurfaceView(
host.getActivity(), host.getTransparencyMode() == TransparencyMode.transparent);
// Allow our host to customize FlutterSurfaceView, if desired.
host.onFlutterSurfaceViewCreated(flutterSurfaceView);
// Create the FlutterView that owns the FlutterSurfaceView.
///用我们的flutterSurfaceView 初始化了一个 FlutterView
flutterView = new FlutterView(host.getActivity(), flutterSurfaceView);
} else {
FlutterTextureView flutterTextureView = new FlutterTextureView(host.getActivity());
// Allow our host to customize FlutterSurfaceView, if desired.
host.onFlutterTextureViewCreated(flutterTextureView);
// Create the FlutterView that owns the FlutterTextureView.
flutterView = new FlutterView(host.getActivity(), flutterTextureView);
}
// Add listener to be notified when Flutter renders its first frame.
flutterView.addOnFirstFrameRenderedListener(flutterUiDisplayListener);
/// 创建一个闪屏view - FlutterSplashView