Flutter之Android层面源码分析(一)

学习Flutter过程当中,先撸了一遍Flutter,写了个仿boss直聘的demo, github地址:flutter_boss. 写完以后其实比较迷茫,android里到底干了啥,因而稍微看了一下源码,有种恍然大悟的感受。android

在建立完Flutter工程后,自动为咱们生成了一个FlutterApplication和一个kotlin的Activity。 在FlutterApplication里其实就作了一件事,经过调用FlutterMain里的startInitialization方法进行初始化。 在生成的主的Activity里咱们能够看见如下内容。git

class MainActivity(): FlutterActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
  }
}
复制代码

能够看到,这个MainActivity就是启动Activity,只不过是继承的FlutterActivity,因而进入FlutterActivity,发现是继承自Activity,而FlutterActivity里的生命周期是委托给另外一个FlutterActivityDelegate管理的,还有一个类名字叫FlutterFragmentActivity共用了该类。 看源码先抓重点,固然先是看FlutterActivityDelegate的onCreate里作了啥github

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为咱们建立好了,通常不会在这里出问题。bash

而后接下去看是怎么初始化的, 实际项目里,咱们是经过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的启动画面。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;
        }

    }

复制代码

代码太长,有的地方省略了,我认为比较重要的是作了如下几点:ide

  1. 真正建立了FlutterNativeView,里面真正工做的是Framework层的dart
  2. 载入系统默认的MethodChannel,至于MethodChannel是干什么的,官方有一张图能够说明。
    咱们也能够在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清除。动画

时间关系,先了解个大概吧,后面有新的收获再来分享。ui

相关文章
相关标签/搜索