text
stringlengths
1
372
to use a pre-warmed FlutterEngine in a FlutterFragment,
instantiate a FlutterFragment with the withCachedEngine()
factory method.
<code_start>
// somewhere in your app, before your FlutterFragment is needed,
// like in the application class ...
// instantiate a FlutterEngine.
FlutterEngine flutterEngine = new FlutterEngine(context);
// start executing dart code in the FlutterEngine.
flutterEngine.getDartExecutor().executeDartEntrypoint(
DartEntrypoint.createDefault()
);
// cache the pre-warmed FlutterEngine to be used later by FlutterFragment.
FlutterEngineCache
.getinstance()
.put("my_engine_id", flutterEngine);
<code_end>
<code_start>
FlutterFragment.withCachedEngine("my_engine_id").build();
<code_end>
<code_start>
// somewhere in your app, before your FlutterFragment is needed,
// like in the application class ...
// instantiate a FlutterEngine.
val flutterEngine = FlutterEngine(context)
// start executing dart code in the FlutterEngine.
flutterEngine.getDartExecutor().executeDartEntrypoint(
DartEntrypoint.createDefault()
)
// cache the pre-warmed FlutterEngine to be used later by FlutterFragment.
FlutterEngineCache
.getinstance()
.put("my_engine_id", flutterEngine)
<code_end>
<code_start>
FlutterFragment.withCachedEngine("my_engine_id").build()
<code_end>
FlutterFragment internally knows about FlutterEngineCache
and retrieves the pre-warmed FlutterEngine based on the ID
given to withCachedEngine().
by providing a pre-warmed FlutterEngine,
as previously shown, your app renders the
first flutter frame as quickly as possible.
<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