學習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
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