text
stringlengths
1
474
flutterFragment!!.onActivityResult(
requestCode,
resultCode,
data
)
}
override fun onUserLeaveHint() {
flutterFragment!!.onUserLeaveHint()
}
override fun onTrimMemory(level: Int) {
super.onTrimMemory(level)
flutterFragment!!.onTrimMemory(level)
}
}<code_end>
With the OS signals forwarded to Flutter,
your FlutterFragment works as expected.
You have now added a FlutterFragment to your existing Android app.The simplest integration path uses a new FlutterEngine,
which comes with a non-trivial initialization time,
leading to a blank UI until Flutter is
initialized and rendered the first time.
Most of this time overhead can be avoided by using
a cached, pre-warmed FlutterEngine, which is discussed next.<topic_end>
<topic_start>
Using a pre-warmed FlutterEngine
By default, a FlutterFragment creates its own instance
of a FlutterEngine, which requires non-trivial warm-up time.
This means your user sees a blank Fragment for a brief moment.
You can mitigate most of this warm-up time by
using an existing, pre-warmed instance of FlutterEngine.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");