text
stringlengths
1
372
.initialroute("/my_route")
.build(currentactivity)
);
}
});
<code_end>
<code_start>
myButton.setOnClickListener {
startActivity(
FlutterActivity
.withnewengine()
.initialroute("/my_route")
.build(this)
)
}
<code_end>
replace "/my_route" with your desired initial route.
the use of the withNewEngine() factory method
configures a FlutterActivity that internally create
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() {