text
stringlengths
1
474
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.info Note
Flutter’s debug/release builds have drastically different
performance characteristics. To evaluate the performance
of Flutter, use a release build.<topic_end>
<topic_start>Initial route with a cached engine
The concept of an initial route is available when configuring a
FlutterActivity or a FlutterFragment with a new FlutterEngine.
However, FlutterActivity and FlutterFragment don’t offer the
concept of an initial route when using a cached engine.
This is because a cached engine is expected to already be
running Dart code, which means it’s too late to configure the
initial route.Developers that would like their cached engine to begin
with a custom initial route can configure their cached
FlutterEngine to use a custom initial route just before
executing the Dart entrypoint. The following example
demonstrates the use of an initial route with a cached engine:
<code_start>public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Instantiate a FlutterEngine.
flutterEngine = new FlutterEngine(this);
// Configure an initial route.
flutterEngine.getNavigationChannel().setInitialRoute("your/route/here");
// Start executing Dart code to pre-warm the FlutterEngine.
flutterEngine.getDartExecutor().executeDartEntrypoint(
DartEntrypoint.createDefault()
);
// Cache the FlutterEngine to be used by FlutterActivity or FlutterFragment.
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)
// Configure an initial route.
flutterEngine.navigationChannel.setInitialRoute("your/route/here");
// Start executing Dart code to pre-warm the FlutterEngine.
flutterEngine.dartExecutor.executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
)
// Cache the FlutterEngine to be used by FlutterActivity or FlutterFragment.
FlutterEngineCache
.getInstance()
.put("my_engine_id", flutterEngine)
}
}<code_end>
By setting the initial route of the navigation channel, the associated
FlutterEngine displays the desired route upon initial execution of the
runApp() Dart function.Changing the initial route property of the navigation channel
after the initial execution of runApp() has no effect.
Developers who would like to use the same FlutterEngine
between different Activitys and Fragments and switch
the route between those displays need to set up a method channel and
explicitly instruct their Dart code to change Navigator routes.<topic_end>
<topic_start>
Add a translucent Flutter screen
Most full-screen Flutter experiences are opaque.
However, some apps would like to deploy a Flutter
screen that looks like a modal, for example,
a dialog or bottom sheet. Flutter supports translucent
FlutterActivitys out of the box.To make your FlutterActivity translucent,
make the following changes to the regular process of
creating and launching a FlutterActivity.<topic_end>
<topic_start>
Step 1: Use a theme with translucency
Android requires a special theme property for Activitys that render
with a translucent background. Create or update an Android theme with the
following property:Then, apply the translucent theme to your FlutterActivity.Your FlutterActivity now supports translucency.
Next, you need to launch your FlutterActivity
with explicit transparency support.<topic_end>
<topic_start>
Step 2: Start FlutterActivity with transparency
To launch your FlutterActivity with a transparent background,
pass the appropriate BackgroundMode to the IntentBuilder:
<code_start>// Using a new FlutterEngine.
startActivity(
FlutterActivity
.withNewEngine()
.backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent)
.build(context)
);
// Using a cached FlutterEngine.
startActivity(
FlutterActivity
.withCachedEngine("my_engine_id")