这里写下调研结果,后续补充内容,
主要是从Flutter跳转至Activity,目前是可以实现的。
在现有的原生项目中嵌入Flutter项目,官方已经给出了例子,如下:
https://flutter.cn/docs/development/add-to-app/android/project-setup
另外一个就是从现有的Flutter项目中跳转到原生页面,这里主要使用混合通信方式进行传递数据跳转,Android部分简单代码如下:
MainActivity.kt
class MainActivity: FlutterFragmentActivity() {
private val CHANNEL = "DQ"
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
GeneratedPluginRegistrant.registerWith(flutterEngine);
MethodChannel(flutterEngine.dartExecutor.binaryMessenger,CHANNEL).setMethodCallHandler { call, result ->
//回调的内容
if (call.method == "getTest") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
goSecondActivity();
} else {
result.notImplemented()
}
}
}
private fun goSecondActivity() {
Log.e("YM", "开始跳转到新的页面");
val intent = Intent(this,SecondActivity::class.java);
startActivity(intent)
}
}
SecondActivity.kt
class SecondActivity: FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.e("YM", "第二个页面的渲染");
setContentView(R.layout.activity_second);
}
}