One of the biggest problems with the mix of Flutter is the use of Engine. There are several different management solutions for the Engine, including memory, performance, and hybrid stack. For example, create an Engine or use FlutterEngineCache. The following lists the official documents.

Docs. Flutter. Dev/development…

Multiple Flutter Instance is a lightweight way to manage Engine.

This is a relatively new official Engine management scheme. This will create multiple isolated Flutter engines with different data in exchange for improved memory performance of each Engine.

IT is similar to the concept of Thin Clients in IT. Each Thin Client itself is isolated from Multiple Thin Clients, but IT can obtain information and data from the central host. In other words, Multiple Flutter scheme, Should be a compromise among the many options the Flutter team has for Add2App. In some scenarios, feasibility is better than others.

The following is the access process of Multiple Flutter. First, create and initialize the FlutterEngineGroup in Application.

class App : Application() {
    lateinit var engines: FlutterEngineGroup

    override fun onCreate() {
        super.onCreate()
        engines = FlutterEngineGroup(this)
    }
}
Copy the code

Next, create a new Activity that inherits from FlutterActivity and declares in the Manifest file that these are normal operations.

<activity android:name=".flutter.SingleFlutterActivity" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|dens ity|uiMode" android:hardwareAccelerated="true" android:theme="@style/Theme.MultipleFlutters" android:windowSoftInputMode="adjustResize" />Copy the code

Finally, in FlutterActivity, reference Engine.

class SingleFlutterActivity : FlutterActivity() {

    val engine: FlutterEngine by lazy {
        val app = activity.applicationContext as XXApplication
        val dartEntrypoint =
            DartExecutor.DartEntrypoint(
                FlutterInjector.instance().flutterLoader().findAppBundlePath(), "main"
            )
        app.engines.createAndRunEngine(activity, dartEntrypoint)
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        SchemaBookApi.NativeBookApi.setup(flutterEngine.dartExecutor, NativeBookApiImp(this, lifecycleScope))
    }

    override fun provideFlutterEngine(context: Context): FlutterEngine? {
        return engine
    }

    override fun onDestroy() {
        super.onDestroy()
        engine.destroy()
    }
}
Copy the code

The code is relatively simple, the core is to get the Engine, through the FlutterEngineGroup provided createAndRunEngine method, you can get a “Thin Client” from the FlutterEngineGroup.

This solution also solves an important problem of using Multiple Flutter, that is, the initial route problem. With dartEntrypoint, we can customize the jump of the initial route to select the actual jump logic on Multiple different logic.

In Flutter only, we need to add entry functions to different Dartentry points.

void main() => runApp(const MyApp(color: Colors.blue));

@pragma('vm:entry-point')
void topMain() => runApp(const MyApp(color: Colors.green));

@pragma('vm:entry-point')
void bottomMain() => runApp(const MyApp(color: Colors.purple));
Copy the code

In this way, the jump of multi-route logic is realized, and the corresponding function name is the name of dartEntrypoint.

In conjunction with the Pigeon Expressway, the basic architecture for Engine management and data communication was established.

I would like to recommend my website xuyisheng. Top/focusing on Android-Kotlin-flutter welcome you to visit