Sub topic 3
How a Native App Hosts Flutter: Engine and View
This chapter assumes basic Dart and Flutter widgets, plus working knowledge of either Android activities and Gradle or iOS view controllers and CocoaPods. You should also know how to run a native app from Android Studio or Xcode.
The two-part hosting model
In an add-to-app project, the native application remains the main application. Flutter supplies one or more screens inside it. Two objects make this possible:
- A
FlutterEngineruns Flutter code. - A Flutter view displays the rendered Flutter UI inside a native screen.
The engine starts a Dart isolate, loads the Flutter application, runs plugins, and communicates with native code. The view connects that running engine to a window or view hierarchy.
The engine and view have different lifetimes. A view can be removed while the engine keeps running. This lets the host reuse an engine when the user opens another Flutter screen.
| Object | Main responsibility | Typical lifetime |
|---|---|---|
| `FlutterEngine` | Runs Dart, plugins, and platform messages | Can live across screens |
| Android `FlutterActivity` | Native activity that displays Flutter | Usually one screen visit |
| Android `FlutterFragment` or `FlutterView` | Embeds Flutter in an existing layout | Matches its host view |
| iOS `FlutterViewController` | Displays Flutter in a view-controller hierarchy | Usually one presented screen |
What the engine does
When the engine runs, it loads the Flutter runtime and executes Dart code. The Dart code builds a widget tree. Flutter turns that tree into a layer scene, and the engine rasterizes the scene for the device.
The engine also owns the binary messenger used by platform channels. This messenger carries method calls and events between Dart and Kotlin, Java, Swift, or Objective-C.
A host can create an engine when it needs a Flutter screen:
val engine = FlutterEngine(this)
engine.dartExecutor.executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
)Creating and starting an engine takes work. Flutter must initialize the runtime, load the application, and build its first frame. If this happens only after a button tap, the first Flutter screen may appear slowly.
What the view does
The view is the visible part of the integration. It receives rendered frames from the engine and sends user input back to Flutter. It does not normally own the Dart application itself.
On Android, FlutterActivity is a convenient host. It creates or receives an engine and provides an activity lifecycle around the Flutter view:
startActivity(
FlutterActivity
.withCachedEngine("main_engine")
.build(this)
)On iOS, FlutterViewController performs the equivalent role:
let flutterVC = FlutterViewController(
engine: dependencies.engine,
nibName: nil,
bundle: nil
)
present(flutterVC, animated: true)The native host controls navigation into and out of this screen. Flutter controls widgets and routes within the embedded Flutter experience. Decide which system owns each navigation boundary before implementation. Mixing both systems for the same transition often produces duplicate back-button handling or confusing lifecycle events.
Separate or reuse the engine
You can create an engine for each screen, but this increases startup cost and memory use. A common alternative is to start one engine early and store it in a cache:
FlutterEngineCache
.getInstance()
.put("main_engine", engine)Later, FlutterActivity.withCachedEngine("main_engine") attaches a view to the already-running engine.
Reuse is useful when:
- The app opens Flutter screens frequently.
- The Flutter state should remain alive between visits.
- Startup latency matters.
A separate engine is safer when screens need isolated Dart state, different entrypoints, or independent lifecycles. Multiple engines consume additional resources, so measure memory on lower-end devices.
Lifecycle responsibilities
When a view is attached, the host must connect it to the current window and lifecycle. When the view is removed, the host must detach it correctly. The engine may continue running, but it should not retain references to destroyed activities, view controllers, or contexts.
Register platform-channel handlers against the same engine that displays the screen. If a handler is registered on one engine while another engine is attached to the view, Dart calls may appear to do nothing.
Exercise
Why can a FlutterEngine remain alive after its Flutter view is removed, and what benefit does this provide?
Show answer
The engine runs Dart independently from the view. Keeping it alive preserves Flutter state and avoids the initialization cost when another view attaches later.