【Flutter 混合開發】新增 Flutter 到 Android Activity
阿新 • • 發佈:2020-10-28
![](https://img2020.cnblogs.com/other/467322/202010/467322-20201027215713180-299329130.png)
> Flutter 混合開發系列 包含如下:
> - 嵌入原生View-Android
> - 嵌入原生View-iOS
> - 與原生通訊-MethodChannel
> - 與原生通訊-BasicMessageChannel
> - 與原生通訊-EventChannel
> - **新增 Flutter 到 Android Activity**
> - 新增 Flutter 到 Android Fragment
> - 新增 Flutter 到 iOS
>
> 每個工作日分享一篇,歡迎關注、點贊及轉發。
### 建立 Flutter Module
Flutter可以以原始碼或AAR的方法嵌入到Android原生專案,整合流程可以使用 Android Studio 完成,也可以手動完成。強烈建議使用 Android Studio。
首先建立一個 Android 專案,建立一個空的 Activity:
![](https://img2020.cnblogs.com/other/467322/202010/467322-20201027215713769-3263478.png)
Android 專案建立成功後,使用Android Studio 新增Flutter模組,在Android原生專案中點選“File > New > New Module...”,建立 **Flutter Module**:
![](https://img2020.cnblogs.com/other/467322/202010/467322-20201027215714591-2146123315.png)
> **注意**:Android Studio 的版本3.5及以上,Flutter IntelliJ plugin版本42及以上。
在彈出的選擇Module型別的對話方塊中選中Flutter Module,然後點選Next,
![](https://img2020.cnblogs.com/other/467322/202010/467322-20201027215715130-1392609478.png)
設定Flutter module的Project name、Flutter SDK等,點選Next:
![](https://img2020.cnblogs.com/other/467322/202010/467322-20201027215715658-1536183139.png)
設定Flutter module的包名,點選Finish:
![](https://img2020.cnblogs.com/other/467322/202010/467322-20201027215715972-1572526729.png)
編譯完成後將在當前App目錄下生成Flutter模組的程式碼,目錄結構如下:
![](https://img2020.cnblogs.com/other/467322/202010/467322-20201027215716267-489261289.png)
### 啟動頁載入 Flutter
將 Flutter 頁面載入到 MainActivity(預設啟動頁) 中,修改 MainActivity :
```kotlin
package com.flutter.androidflutter
import io.flutter.embedding.android.FlutterActivity
class MainActivity : FlutterActivity()
```
你沒有看錯,只需讓 MainActivity 繼承 **FlutterActivity** 即可。
> **注意**:FlutterActivity的包名是io.flutter.embedding.android.FlutterActivity
![](https://img2020.cnblogs.com/other/467322/202010/467322-20201027215716856-1471829589.jpg)
### 跳轉到 Flutter 頁面
MainActivity(預設啟動頁)新增一個按鈕,點選後跳轉到新的頁面,此頁面載入 Flutter ,MainActivity程式碼如下:
```kotlin
package com.flutter.androidflutter
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
startActivity(Intent(this,SecondFlutterActivity::class.java))
}
}
}
```
SecondFlutterActivity 程式碼如下:
```kotlin
package com.flutter.androidflutter
import io.flutter.embedding.android.FlutterActivity
class SecondFlutterActivity:FlutterActivity()
```
在 **AndroidManifest.xml** 中註冊此 Activity:
```
```
![flutter_android_1](https://img2020.cnblogs.com/other/467322/202010/467322-20201027215717156-1899099228.gif)
**SecondFlutterActivity** 只是繼承了 **FlutterActivity**,這種情況下,也可以直接使用 **FlutterActivity**:
```kotlin
startActivity(Intent(this, FlutterActivity::class.java))
```
或者:
```kotlin
startActivity(FlutterActivity.createDefaultIntent(this))
```
在 **AndroidManifest.xml** 中註冊 FlutterActivity