您现在的位置是:首页 >技术交流 >Flutter之Android层面源码分析(一)网站首页技术交流
Flutter之Android层面源码分析(一)
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;
}
}
代码太长,有的地方省略了,我认为比较重要的是做了以下几点:
- 真正创建了FlutterNativeView,里面真正工作的是Framework层的dart
- 载入系统默认的MethodChannel,至于MethodChannel是干什么的,官方有一张图可以说明。
我们也可以在Activity里做一些扩张,自定义自己的MethodChannel
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
// TODO
}
});
其他的activity里的几个生命周期方法,没什么好说的,简单说下onDestroy,
call, Result result) {
// TODO
}
});
其他的activity里的几个生命周期方法,没什么好说的,简单说下onDestroy,