text
stringlengths
1
474
its own FlutterEngine instance. This comes with a
non-trivial initialization time. The alternative approach
is to instruct FlutterActivity to use a pre-warmed,
cached FlutterEngine, which minimizes Flutter’s
initialization time. That approach is discussed next.<topic_end>
<topic_start>
Step 3: (Optional) Use a cached FlutterEngine
Every FlutterActivity creates its own FlutterEngine
by default. Each FlutterEngine has a non-trivial
warm-up time. This means that launching a standard
FlutterActivity comes with a brief delay before your Flutter
experience becomes visible. To minimize this delay,
you can warm up a FlutterEngine before arriving at
your FlutterActivity, and then you can use
your pre-warmed FlutterEngine instead.To pre-warm a FlutterEngine, find a reasonable
location in your app to instantiate a FlutterEngine.
The following example arbitrarily pre-warms a
FlutterEngine in the Application class:
<code_start>public class MyApplication extends Application {
public FlutterEngine flutterEngine;
@Override
public void onCreate() {
super.onCreate();
// Instantiate a FlutterEngine.
flutterEngine = new FlutterEngine(this);
// Start executing Dart code to pre-warm the FlutterEngine.
flutterEngine.getDartExecutor().executeDartEntrypoint(
DartEntrypoint.createDefault()
);
// Cache the FlutterEngine to be used by FlutterActivity.
FlutterEngineCache
.getInstance()
.put("my_engine_id", flutterEngine);
}
}<code_end>
<code_start>class MyApplication : Application() {
lateinit var flutterEngine : FlutterEngine
override fun onCreate() {
super.onCreate()
// Instantiate a FlutterEngine.
flutterEngine = FlutterEngine(this)
// Start executing Dart code to pre-warm the FlutterEngine.
flutterEngine.dartExecutor.executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
)
// Cache the FlutterEngine to be used by FlutterActivity.
FlutterEngineCache
.getInstance()
.put("my_engine_id", flutterEngine)
}
}<code_end>
The ID passed to the FlutterEngineCache can be whatever you want.
Make sure that you pass the same ID to any FlutterActivity
or FlutterFragment that should use the cached FlutterEngine.
Using FlutterActivity with a cached FlutterEngine
is discussed next.info Note
To warm up a FlutterEngine, you must execute a Dart
entrypoint. Keep in mind that the moment
executeDartEntrypoint() is invoked,
your Dart entrypoint method begins executing.
If your Dart entrypoint invokes runApp()
to run a Flutter app, then your Flutter app behaves as if it
were running in a window of zero size until this
FlutterEngine is attached to a FlutterActivity,
FlutterFragment, or FlutterView. Make sure that your app
behaves appropriately between the time you warm it up and
the time you display Flutter content.With a pre-warmed, cached FlutterEngine, you now need
to instruct your FlutterActivity to use the cached
FlutterEngine instead of creating a new one.
To accomplish this, use FlutterActivity’s withCachedEngine()
builder:
<code_start>myButton.addOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(
FlutterActivity
.withCachedEngine("my_engine_id")
.build(currentActivity)
);
}
});<code_end>
<code_start>myButton.setOnClickListener {
startActivity(
FlutterActivity
.withCachedEngine("my_engine_id")
.build(this)
)
}<code_end>
When using the withCachedEngine() factory method,
pass the same ID that you used when caching the desired
FlutterEngine.Now, when you launch FlutterActivity,
there is significantly less delay in
the display of Flutter content.info 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().info Note