}
很简单,继续往里走
public static void startInitialization(@NonNull Context applicationContext) {
if (isRunningInRobolectricTest) {
return;
}
FlutterLoader.getInstance().startInitialization(applicationContext);
}
上面的方法用于初始化 native system(即C++),并最终会调用下面的方法:
我将说明以注释的形式写在下面
public void startInitialization(@NonNull Context applicationContext, @NonNull Settings settings) {
if (this.settings != null) {
return;
}
///确保运行在 主线程
if (Looper.myLooper() != Looper.getMainLooper()) {
throw new IllegalStateException(“startInitialization must be called on the main thread”);
}
// Ensure that the context is actually the application context.
final Context appContext = applicationContext.getApplicationContext();
this.settings = settings;
initStartTimestampMillis = SystemClock.uptimeMillis();
///配置 aotSharedLibraryName、flutterAssetsDir、
/// vmSnapshotData、isolateSnapshotData
///等参数
initConfig(appContext);
///初始化VsyncWaiter,并设置回调
/// 当vsync信号到来时,就调用我们设置的回调,最终会触发页面的刷新
VsyncWaiter.getInstance((WindowManager) appContext.getSystemService(Context.WINDOW_SERVICE))
.init();
// 子线程
///这里主要是抽取资源文件,
///加载 flutter(libflutter.so)代码
Callable initTask =
new Callable() {
@Override
public InitResult call() {
ResourceExtractor resourceExtractor = initResources(appContext);
System.loadLibrary(“flutter”);
// Prefetch the default font manager as soon as possible on a background thread.
// It helps to reduce time cost of engine setup that blocks the platform thread.
Executors.newSingleThreadExecutor()
.execute(
new Runnable() {
@Override
public void run() {
FlutterJNI.nativePrefetchDefaultFontManager();
}
});
if (resourceExtractor != null) {
resourceExtractor.waitForCompletion();
}
return new InitResult(
PathUtils.getFilesDir(appContext),
PathUtils.getCacheDirectory(appContext),
PathUtils.getDataDirectory(appContext));
}
};
initResultFuture = Executors.newSingleThreadExecutor().submit(initTask);
}
至此FlutterApplication 的相关流程就走完了。
另外,虽然上面的代码中使用了子线程,但是最终在这些任务没有完成前,是不会进入flutter侧的,我们接着走FlutterActivity。
FlutterActivity & onCreate
开始的地方依然是 onCreate()方法:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
///切换主题
switchLaunchThemeForNormalTheme();
super.onCreate(savedInstanceState);
///通知生命周期
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
///初始化delete,这个很重要,
///所有的工作都是由它来完成的
delegate = new FlutterActivityAndFragmentDelegate(this);
delegate.onAttach(this);
///是否需要恢复(包括通知插件)一些状态
delegate.onActivityCreated(savedInstanceState);
///配置窗口
configureWindowForTransparency();
///创建flutterView
setContentView(createFlutterView());
configureStatusBarForFullscreenFlutterExperience();
}
这里面比较重的代码是这几行:
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 */);
}
最终会调用 delete的onCreateView :
@NonNull
View onCreateView(
LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.v(TAG, “Creating FlutterView.”);
ensureAlive();
if (host.getRenderMode() == RenderMode.surface) {
///一般flutter应用是 RenderMode.surface,所以会进入到这里
///创建FlutterSurfaceView
FlutterSurfaceView flutterSurfaceView =
new FlutterSurfaceView(
host.getActivity(), host.getTransparencyMode() == TransparencyMode.transparent);
// Allow our host to customize FlutterSurfaceView, if desired.
host.onFlutterSurfaceViewCreated(flutterSurfaceView);
// flutterView 创建完成后,便会调用addView
//将 flutterSurfaceView 显示出来,只不过啥都没有而已
flutterView = new FlutterView(host.getActivity(), flutterSurfaceView);
} else {
…省略代码…
}
// Add listener to be notified when Flutter renders its first frame.
flutterView.addOnFirstFrameRenderedListener(flutterUiDisplayListener);
flutterSplashView = new FlutterSplashView(host.getContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
flutterSplashView.setId(View.generateViewId());
} else {
flutterSplashView.setId(486947586);
}
///这里显示闪屏页 默认是个白屏
///即,AndroidMainfest.xml 的
flutterSplashView.displayFlutterViewWithSplash(flutterView, host.provideSplashScreen());
///将flutterview 绑定到 engine上
flutterView.attachToFlutterEngine(flutterEngine);
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
尾声
一转眼时间真的过的飞快。我们各奔东西,也各自踏上了自己的旅途,但是即使多年不见,也因为这份情谊我们依旧如从前那般“亲密”。不忘初心方得始终。加油吧,程序员们,在我看来35岁,40岁从来不是危机,只要永远不要忘记自己为何踏上征程!
为了让更多在学习中或者最近要准备面试的朋友们看到这篇文章,希望你们能多多评论,点赞+转发!
再次感谢所有给我提供过题目的朋友们,感谢一路有你!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
尾声
一转眼时间真的过的飞快。我们各奔东西,也各自踏上了自己的旅途,但是即使多年不见,也因为这份情谊我们依旧如从前那般“亲密”。不忘初心方得始终。加油吧,程序员们,在我看来35岁,40岁从来不是危机,只要永远不要忘记自己为何踏上征程!
为了让更多在学习中或者最近要准备面试的朋友们看到这篇文章,希望你们能多多评论,点赞+转发!
再次感谢所有给我提供过题目的朋友们,感谢一路有你!