Open Flutter module from Android App

tanya anand
3 min readApr 28, 2022

Understand how we can open a flutter module from the host android app.

Flutter can be added to the android app in three different ways:

  1. Using FlutterActivity (io.flutter.embedding.android.FlutterActivity)
  2. Using FlutterFragment (io.flutter.embedding.android.FlutterFragment)
  3. Using FlutterView (io.flutter.embedding.android.FlutterView)

Here, we are going to use FlutterActivity to display flutter screens.

Flutter provides FlutterActivity to display a Flutter experience within an Android app. Like any other activity, FlutterActivity must be registered in your AndroidManifest.xml.

Step 1:

Add the following XML to your AndroidManifest.xml file under your application tag:

<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"/>

Step 2:

Launch your FlutterActivity, on a click of a button

myButton.setOnClickListener {
startActivity(
FlutterActivity
.withNewEngine()
.build(this)
)
}

The above example assumes that your Dart entry point is called main(), and the initial Flutter route is ‘/’. The Dart entry point can’t be changed using Intent, but the initial route can be changed using Intent.

Let's see how we can change the initial route.

myButton.setOnClickListener {
startActivity(
FlutterActivity
.withNewEngine()
.initialRoute("/flutter_route")
.build(this)
)
}

Now, our initial route will be ‘/flutter_route’. you can replace it with any desired value you want.

The use of the withNewEngine() method configures a FlutterActivity that internally creates its own FlutterEngine instance. Also, the FlutterEngine takes some time to start up. which leads to a blank screen for a while.

Ohk! now, what else can be done?

Alternative approach

The alternative approach is to use a pre-warmed, cached FlutterEngine.

Every FlutterActivity needs its own FlutterEngine by default. Each FlutterEngine has a non-trivial warm-up time. This means that launching a standard FlutterActivity will have a brief delay before becoming visible. To minimize this delay, we need to warm up FlutterEngine before launching FlutterActivity.

Let's understand, how we can do this.

To pre-warm a FlutterEngine, we need to find a location in the app, where we can do this. A good location can be any activity or an Application class.

destroyEngineWithActivity will kill your engine every time you kill flutter Activity. You can use or avoid depending on your need.

Once FlutterEngine is created, every time you use the same flutter engine to launch FlutterActivity, the state remains retained. Sometimes we don't want this. Thus we have to create a new flutter engine every time and destroy on flutter activity destruction.

In order to remove all FlutterEngine from the cache, we can use the below command.

// clear all flutterEngine instance stored in cache
FlutterEngineCache.getInstance().clear()

The ID passed to the FlutterEngineCache can be whatever one wants. Make sure that ID is the same when you start FlutterActivity.

Adding some notes from the official site

Note: When using a cached FlutterEngine, that FlutterEngine outlives any FlutterActivity or FlutterFragment that displays it. Keep in mind that Dart code begins executing as soon as you pre-warm the FlutterEngine, and continues executing after the destruction of your FlutterActivity/FlutterFragment. To stop executing and clear resources, obtain your FlutterEngine from the FlutterEngineCache and destroy the FlutterEngine with FlutterEngine.destroy().

Note: Runtime performance isn’t the only reason that you might pre-warm and cache a FlutterEngine. A pre-warmed FlutterEngine executes Dart code independent from a FlutterActivity, which allows such a FlutterEngine to be used to execute arbitrary Dart code at any moment. Non-UI application logic can be executed in a FlutterEngine, like networking and data caching, and in background behavior within a Service or elsewhere. When using a FlutterEngine to execute behavior in the background, be sure to adhere to all Android restrictions on background execution.

Thanks! for your precious time. Hope this will help you.

--

--