//BaseActivity必须继承AppCompatActivity,才有ActivityLifeCycle。
class FlutterTestActivity : BaseActivity() {
override fun setLayout() = R.layout.m_activity_flutter_test
private val flutterEngineId = "flutterEngineId"
private var engine: FlutterEngine? = null
override fun initView(view: View) {
setToolbarTitle("flutter测试页面")
engine = FlutterEngineCache.getInstance().get(flutterEngineId)
if (engine == null) {
engine = FlutterEngine(this)
engine?.dartExecutor?.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
FlutterEngineCache.getInstance().put(flutterEngineId, engine)
}
val view = createFlutterView()
view.attachToFlutterEngine(engine!!)
}
private fun createFlutterView(): FlutterView {
val flutterView = FlutterView(this) //默认加载flutter侧的main.dart文件
val params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
binding.flContain.addView(flutterView, params)
return flutterView
}
override fun initEvent() {
}
override fun createPresenter() = FlutterTestPresenter()
override fun onResume() {
super.onResume()
engine?.lifecycleChannel?.appIsResumed()
}
override fun onPause() {
super.onPause()
engine?.lifecycleChannel?.appIsInactive()
}
override fun onStop() {
super.onStop()
engine?.lifecycleChannel?.appIsPaused()
}
}