9
9
import static io .flutter .embedding .android .FlutterActivityLaunchConfigs .DEFAULT_DART_ENTRYPOINT ;
10
10
import static io .flutter .embedding .android .FlutterActivityLaunchConfigs .DEFAULT_INITIAL_ROUTE ;
11
11
import static io .flutter .embedding .android .FlutterActivityLaunchConfigs .EXTRA_BACKGROUND_MODE ;
12
+ import static io .flutter .embedding .android .FlutterActivityLaunchConfigs .EXTRA_CACHED_ENGINE_GROUP_ID ;
12
13
import static io .flutter .embedding .android .FlutterActivityLaunchConfigs .EXTRA_CACHED_ENGINE_ID ;
14
+ import static io .flutter .embedding .android .FlutterActivityLaunchConfigs .EXTRA_DART_ENTRYPOINT ;
13
15
import static io .flutter .embedding .android .FlutterActivityLaunchConfigs .EXTRA_DESTROY_ENGINE_WITH_ACTIVITY ;
14
16
import static io .flutter .embedding .android .FlutterActivityLaunchConfigs .EXTRA_ENABLE_STATE_RESTORATION ;
15
17
import static io .flutter .embedding .android .FlutterActivityLaunchConfigs .EXTRA_INITIAL_ROUTE ;
@@ -269,7 +271,7 @@ public NewEngineIntentBuilder(@NonNull Class<? extends FlutterActivity> activity
269
271
}
270
272
271
273
/**
272
- * The initial route that a Flutter app will render in this {@link FlutterFragment }, defaults to
274
+ * The initial route that a Flutter app will render in this {@link FlutterActivity }, defaults to
273
275
* "/".
274
276
*
275
277
* @param initialRoute The route.
@@ -421,6 +423,120 @@ public Intent build(@NonNull Context context) {
421
423
}
422
424
}
423
425
426
+ /**
427
+ * Creates a {@link NewEngineInGroupIntentBuilder}, which can be used to configure an {@link
428
+ * Intent} to launch a {@code FlutterActivity} that internally uses an existing {@link
429
+ * io.flutter.embedding.engine.FlutterEngineGroup} that is cached in {@link
430
+ * io.flutter.embedding.engine.FlutterEngineGroupCache}. and creates a new {@link
431
+ * io.flutter.embedding.engine.FlutterEngine} by FlutterEngineGroup#createAndRunEngine
432
+ *
433
+ * @param engineGroupId A cached engine group ID.
434
+ * @return The builder.
435
+ */
436
+ public static NewEngineInGroupIntentBuilder withNewEngineInGroup (@ NonNull String engineGroupId ) {
437
+ return new NewEngineInGroupIntentBuilder (FlutterActivity .class , engineGroupId );
438
+ }
439
+
440
+ /**
441
+ * Builder to create an {@code Intent} that launches a {@code FlutterActivity} with a new {@link
442
+ * FlutterEngine} by FlutterEngineGroup#createAndRunEngine.
443
+ */
444
+ public static class NewEngineInGroupIntentBuilder {
445
+ private final Class <? extends FlutterActivity > activityClass ;
446
+ private final String cachedEngineGroupId ;
447
+ private String dartEntrypoint = DEFAULT_DART_ENTRYPOINT ;
448
+ private String initialRoute = DEFAULT_INITIAL_ROUTE ;
449
+ private String backgroundMode = DEFAULT_BACKGROUND_MODE ;
450
+
451
+ /**
452
+ * Constructor that allows this {@code NewEngineInGroupIntentBuilder} to be used by subclasses
453
+ * of {@code FlutterActivity}.
454
+ *
455
+ * <p>Subclasses of {@code FlutterActivity} should provide their own static version of {@link
456
+ * #withNewEngineInGroup}, which returns an instance of {@code NewEngineInGroupIntentBuilder}
457
+ * constructed with a {@code Class} reference to the {@code FlutterActivity} subclass, e.g.:
458
+ *
459
+ * <p>{@code return new NewEngineInGroupIntentBuilder(MyFlutterActivity.class,
460
+ * cacheedEngineGroupId); }
461
+ *
462
+ * @param activityClass A subclass of {@code FlutterActivity}.
463
+ * @param engineGroupId The engine group id.
464
+ */
465
+ public NewEngineInGroupIntentBuilder (
466
+ @ NonNull Class <? extends FlutterActivity > activityClass , @ NonNull String engineGroupId ) {
467
+ this .activityClass = activityClass ;
468
+ this .cachedEngineGroupId = engineGroupId ;
469
+ }
470
+
471
+ /**
472
+ * The Dart entrypoint that will be executed as soon as the Dart snapshot is loaded, default to
473
+ * "main".
474
+ *
475
+ * @param dartEntrypoint The dart entrypoint's name
476
+ * @return The engine group intent builder
477
+ */
478
+ @ NonNull
479
+ public NewEngineInGroupIntentBuilder dartEntrypoint (@ NonNull String dartEntrypoint ) {
480
+ this .dartEntrypoint = dartEntrypoint ;
481
+ return this ;
482
+ }
483
+
484
+ /**
485
+ * The initial route that a Flutter app will render in this {@link FlutterActivity}, defaults to
486
+ * "/".
487
+ *
488
+ * @param initialRoute The route.
489
+ * @return The engine group intent builder.
490
+ */
491
+ @ NonNull
492
+ public NewEngineInGroupIntentBuilder initialRoute (@ NonNull String initialRoute ) {
493
+ this .initialRoute = initialRoute ;
494
+ return this ;
495
+ }
496
+
497
+ /**
498
+ * The mode of {@code FlutterActivity}'s background, either {@link BackgroundMode#opaque} or
499
+ * {@link BackgroundMode#transparent}.
500
+ *
501
+ * <p>The default background mode is {@link BackgroundMode#opaque}.
502
+ *
503
+ * <p>Choosing a background mode of {@link BackgroundMode#transparent} will configure the inner
504
+ * {@link FlutterView} of this {@code FlutterActivity} to be configured with a {@link
505
+ * FlutterTextureView} to support transparency. This choice has a non-trivial performance
506
+ * impact. A transparent background should only be used if it is necessary for the app design
507
+ * being implemented.
508
+ *
509
+ * <p>A {@code FlutterActivity} that is configured with a background mode of {@link
510
+ * BackgroundMode#transparent} must have a theme applied to it that includes the following
511
+ * property: {@code <item name="android:windowIsTranslucent">true</item>}.
512
+ *
513
+ * @param backgroundMode The background mode.
514
+ * @return The engine group intent builder.
515
+ */
516
+ @ NonNull
517
+ public NewEngineInGroupIntentBuilder backgroundMode (@ NonNull BackgroundMode backgroundMode ) {
518
+ this .backgroundMode = backgroundMode .name ();
519
+ return this ;
520
+ }
521
+
522
+ /**
523
+ * Creates and returns an {@link Intent} that will launch a {@code FlutterActivity} with the
524
+ * desired configuration.
525
+ *
526
+ * @param context The context. e.g. An Activity.
527
+ * @return The intent.
528
+ */
529
+ @ NonNull
530
+ public Intent build (@ NonNull Context context ) {
531
+ return new Intent (context , activityClass )
532
+ .putExtra (EXTRA_DART_ENTRYPOINT , dartEntrypoint )
533
+ .putExtra (EXTRA_INITIAL_ROUTE , initialRoute )
534
+ .putExtra (EXTRA_CACHED_ENGINE_GROUP_ID , cachedEngineGroupId )
535
+ .putExtra (EXTRA_BACKGROUND_MODE , backgroundMode )
536
+ .putExtra (EXTRA_DESTROY_ENGINE_WITH_ACTIVITY , true );
537
+ }
538
+ }
539
+
424
540
// Delegate that runs all lifecycle and OS hook logic that is common between
425
541
// FlutterActivity and FlutterFragment. See the FlutterActivityAndFragmentDelegate
426
542
// implementation for details about why it exists.
@@ -775,6 +891,17 @@ public String getCachedEngineId() {
775
891
return getIntent ().getStringExtra (EXTRA_CACHED_ENGINE_ID );
776
892
}
777
893
894
+ /**
895
+ * Returns the ID of a statically cached {@link io.flutter.embedding.engine.FlutterEngineGroup} to
896
+ * use within this {@code FlutterActivity}, or {@code null} if this {@code FlutterActivity} does
897
+ * not want to use a cached {@link io.flutter.embedding.engine.FlutterEngineGroup}.
898
+ */
899
+ @ Override
900
+ @ Nullable
901
+ public String getCachedEngineGroupId () {
902
+ return getIntent ().getStringExtra (EXTRA_CACHED_ENGINE_GROUP_ID );
903
+ }
904
+
778
905
/**
779
906
* Returns false if the {@link io.flutter.embedding.engine.FlutterEngine} backing this {@code
780
907
* FlutterActivity} should outlive this {@code FlutterActivity}, or true to be destroyed when the
@@ -801,14 +928,26 @@ public boolean shouldDestroyEngineWithHost() {
801
928
/**
802
929
* The Dart entrypoint that will be executed as soon as the Dart snapshot is loaded.
803
930
*
804
- * <p>This preference can be controlled by setting a {@code <meta-data>} called {@link
805
- * FlutterActivityLaunchConfigs#DART_ENTRYPOINT_META_DATA_KEY} within the Android manifest
806
- * definition for this {@code FlutterActivity}.
931
+ * <p>This preference can be controlled with 2 methods:
932
+ *
933
+ * <ol>
934
+ * <li>Pass a boolean as {@link FlutterActivityLaunchConfigs#EXTRA_DART_ENTRYPOINT} with the
935
+ * launching {@code Intent}, or
936
+ * <li>Set a {@code <meta-data>} called {@link
937
+ * FlutterActivityLaunchConfigs#DART_ENTRYPOINT_META_DATA_KEY} within the Android manifest
938
+ * definition for this {@code FlutterActivity}
939
+ * </ol>
940
+ *
941
+ * If both preferences are set, the {@code Intent} preference takes priority.
807
942
*
808
943
* <p>Subclasses may override this method to directly control the Dart entrypoint.
809
944
*/
810
945
@ NonNull
811
946
public String getDartEntrypointFunctionName () {
947
+ if (getIntent ().hasExtra (EXTRA_DART_ENTRYPOINT )) {
948
+ return getIntent ().getStringExtra (EXTRA_DART_ENTRYPOINT );
949
+ }
950
+
812
951
try {
813
952
Bundle metaData = getMetaData ();
814
953
String desiredDartEntrypoint =
0 commit comments