学习Flutter过程中,先撸了一遍Flutter,写了个仿boss直聘的demo, github地址:flutter_boss. 写完之后其实比较迷茫,android里到底干了啥,于是稍微看了一下源码,有种恍然大悟的感觉。
在创建完Flutter工程后,自动为我们生成了一个FlutterApplication和一个kotlin的Activity。 在FlutterApplication里其实就做了一件事,通过调用FlutterMain里的startInitialization方法进行初始化。 在生成的主的Activity里我们可以看见以下内容。
class MainActivity(): FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
}
}
可以看到,这个MainActivity就是启动Activity,只不过是继承的FlutterActivity,于是进入FlutterActivity,发现是继承自Activity,而FlutterActivity里的生命周期是委托给另一个FlutterActivityDelegate管理的,还有一个类名字叫FlutterFragmentActivity共用了该类。 看源码先抓重点,当然先是看FlutterActivityDelegate的onCreate里做了啥
String[] args = getArgsFromIntent(this.activity.getIntent());
FlutterMain.ensureInitializationComplete(this.activity.getApplicationContext(), args);
this.flutterView = this.viewFactory.createFlutterView(this.activity);
if(this.flutterView == null) {
FlutterNativeView nativeView = this.viewFactory.createFlutterNativeView();
this.flutterView = new FlutterView(this.activity, (AttributeSet)null, nativeView);
this.flutterView.setLayoutParams(matchParent);
this.activity.setContentView(this.flutterView);
this.launchView = this.createLaunchView();
if(this.launchView != null) {
this.addLaunchView();
}
}
前2行是看方法意思是关于确保了Flutter环境初始化完成,如果初始化失败,则会提示"Flutter initialization failed."并抛出RuntimeException,这块不用暂时不用太关心,Flutter工程IDE为我们创建好了,一般不会在这里出问题。
然后接下去看是怎么初始化的, 实际项目里,我们是通过Dart来编写Flutter界面的,那么我们肯定最关心Flutter和activity里的界面是什么关系,怎么承载的。通过初始化我们可以看到flutterView是通过viewFactory接口里的2个方法createFlutterView和createFlutterNativeView里去创建,默认是直接返回null,这么写的目的是可以通过override由自己传入。接下去看,由于默认的flutterView是null,所以就通过new FlutterView创建。其实FlutterView继承自SurfaceView,这时候,Android自定义View的知识派上用处了。 最后通过最熟悉的activity.setContentView(this.flutterView);设置完成。所以我们可以得出一个结论,Flutter开发出来的应用不管里面有多少个界面,都是一个继承自SurfaceView的FlutterView,既不是activity也不是fragment,只是一个view,必要时,我们可以重写FlutterActivityDelegate里的onCreate实现我们自己的需求。 注意后面的createLaunchView方法,我们可以创建app的启动画面。
public FlutterView(Context context, AttributeSet attrs, FlutterNativeView nativeView) {
super(context, attrs);
...
Activity activity = (Activity)this.getContext();
if(nativeView == null) {
this.mNativeView = new FlutterNativeView(activity.getApplicationContext());
} else {
this.mNativeView = nativeView;
}
this.mNativeView.attachViewAndActivity(this, activity);
...
this.mAccessibilityManager = (AccessibilityManager)this.getContext().getSystemService("accessibility");
this.mActivityLifecycleListeners = new ArrayList();
this.mFirstFrameListeners = new ArrayList();
this.mFlutterLocalizationChannel = new MethodChannel(this, "flutter/localization", JSONMethodCodec.INSTANCE);
this.mFlutterNavigationChannel = new MethodChannel(this, "flutter/navigation", JSONMethodCodec.INSTANCE);
this.mFlutterKeyEventChannel = new BasicMessageChannel(this, "flutter/keyevent", JSONMessageCodec.INSTANCE);
this.mFlutterLifecycleChannel = new BasicMessageChannel(this, "flutter/lifecycle", StringCodec.INSTANCE);
this.mFlutterSystemChannel = new BasicMessageChannel(this, "flutter/system", JSONMessageCodec.INSTANCE);
this.mFlutterSettingsChannel = new BasicMessageChannel(this, "flutter/settings", JSONMessageCodec.INSTANCE);
PlatformPlugin platformPlugin = new PlatformPlugin(activity);
MethodChannel flutterPlatformChannel = new MethodChannel(this, "flutter/platform", JSONMethodCodec.INSTANCE);
flutterPlatformChannel.setMethodCallHandler(platformPlugin);
this.addActivityLifecycleListener(platformPlugin);
this.mTextInputPlugin = new TextInputPlugin(this);
this.setLocale(this.getResources().getConfiguration().locale);
this.setUserSettings();
if((context.getApplicationInfo().flags & 2) != 0) {
this.mDiscoveryReceiver = new FlutterView.DiscoveryReceiver(null);
context.registerReceiver(this.mDiscoveryReceiver, new IntentFilter("io.flutter.view.DISCOVER"));
} else {
this.mDiscoveryReceiver = null;
}
}
代码太长,有的地方省略了,我认为比较重要的是做了以下几点:
我们也可以在Activity里做一些扩张,自定义自己的MethodChannel
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
// TODO
}
});
其他的activity里的几个生命周期方法,没什么好说的,简单说下onDestroy,
public void destroy() {
if(this.isAttached()) {
if(this.mDiscoveryReceiver != null) {
this.getContext().unregisterReceiver(this.mDiscoveryReceiver);
}
this.getHolder().removeCallback(this.mSurfaceCallback);
this.mNativeView.destroy();
this.mNativeView = null;
}
}
就是把FlutterNativeView清除。
时间关系,先了解个大概吧,后面有新的收获再来分享。