Flutter Engine
The Flutter Engine
Classes | Public Member Functions | Package Functions | Package Attributes | List of all members
io.flutter.embedding.android.FlutterActivityAndFragmentDelegate Class Reference
Inheritance diagram for io.flutter.embedding.android.FlutterActivityAndFragmentDelegate:
io.flutter.embedding.android.ExclusiveAppComponent< Activity >

Classes

interface  DelegateFactory
 
interface  Host
 

Public Member Functions

Activity getAppComponent ()
 
void detachFromFlutterEngine ()
 
- Public Member Functions inherited from io.flutter.embedding.android.ExclusiveAppComponent< Activity >
void detachFromFlutterEngine ()
 
T getAppComponent ()
 

Package Functions

 FlutterActivityAndFragmentDelegate (@NonNull Host host)
 
 FlutterActivityAndFragmentDelegate (@NonNull Host host, @Nullable FlutterEngineGroup engineGroup)
 
void release ()
 
FlutterEngine getFlutterEngine ()
 
boolean isFlutterEngineFromHost ()
 
boolean isAttached ()
 
void onAttach (@NonNull Context context)
 
void setUpFlutterEngine ()
 
View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState, int flutterViewId, boolean shouldDelayFirstAndroidViewDraw)
 
void onRestoreInstanceState (@Nullable Bundle bundle)
 
void onStart ()
 
void onResume ()
 
void onPostResume ()
 
void updateSystemUiOverlays ()
 
void onPause ()
 
void onStop ()
 
void onDestroyView ()
 
void onSaveInstanceState (@Nullable Bundle bundle)
 
void onDetach ()
 
void onBackPressed ()
 
void startBackGesture (@NonNull BackEvent backEvent)
 
void updateBackGestureProgress (@NonNull BackEvent backEvent)
 
void commitBackGesture ()
 
void cancelBackGesture ()
 
void onRequestPermissionsResult (int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
 
void onNewIntent (@NonNull Intent intent)
 
void onActivityResult (int requestCode, int resultCode, Intent data)
 
void onUserLeaveHint ()
 
void onWindowFocusChanged (boolean hasFocus)
 
void onTrimMemory (int level)
 

Package Attributes

FlutterView flutterView
 
OnPreDrawListener activePreDrawListener
 

Detailed Description

Delegate that implements all Flutter logic that is the same between a FlutterActivity and a FlutterFragment.

Why does this class exist?

One might ask why an Activity and Fragment delegate needs to exist. Given that a Fragment can be placed within an Activity, it would make more sense to use a FlutterFragment within a FlutterActivity.

The Fragment support library adds 100k of binary size to an app, and full-Flutter apps do not otherwise require that binary hit. Therefore, it was concluded that Flutter must provide a FlutterActivity based on the AOSP Activity, and an independent FlutterFragment for add-to-app developers.

If a time ever comes where the inclusion of Fragments in a full-Flutter app is no longer deemed an issue, this class should be immediately decomposed between FlutterActivity and FlutterFragment and then eliminated.

Caution when modifying this class

Any time that a "delegate" is created with the purpose of encapsulating the internal behaviors of another object, that delegate is highly susceptible to degeneration. It is easy to tack new responsibilities on to the delegate which would not otherwise be added to the original object. It is also easy to begin hanging listeners and callbacks on a delegate object that likewise would not be added to the original object. A delegate can quickly become a complex web of dependencies and optional references that are very difficult to track.

Maintainers of this class should take care to only place code in this delegate that would otherwise be placed in either FlutterActivity or FlutterFragment, and in exactly the same form. Do not use this class as a convenient shortcut for any other behavior.

Definition at line 75 of file FlutterActivityAndFragmentDelegate.java.

Constructor & Destructor Documentation

◆ FlutterActivityAndFragmentDelegate() [1/2]

io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.FlutterActivityAndFragmentDelegate ( @NonNull Host  host)
inlinepackage

Definition at line 117 of file FlutterActivityAndFragmentDelegate.java.

117 {
118 this(host, null);
119 }

◆ FlutterActivityAndFragmentDelegate() [2/2]

io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.FlutterActivityAndFragmentDelegate ( @NonNull Host  host,
@Nullable FlutterEngineGroup  engineGroup 
)
inlinepackage

Definition at line 121 of file FlutterActivityAndFragmentDelegate.java.

121 {
122 this.host = host;
123 this.isFirstFrameRendered = false;
124 this.engineGroup = engineGroup;
125 }

Member Function Documentation

◆ cancelBackGesture()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.cancelBackGesture ( )
inlinepackage

Invoke this from OnBackAnimationCallback#onBackCancelled().

This method should be called when a back gesture is cancelled or the back button is pressed. It informs the Flutter framework about the cancellation.

This method enables the Flutter framework to rollback any UI changes or animations initiated in response to the back gesture. This includes resetting UI elements to their state prior to the gesture's start.

Definition at line 879 of file FlutterActivityAndFragmentDelegate.java.

879 {
880 ensureAlive();
881 if (flutterEngine != null) {
882 Log.v(TAG, "Forwarding cancelBackGesture() to FlutterEngine.");
884 } else {
885 Log.w(TAG, "Invoked cancelBackGesture() before FlutterFragment was attached to an Activity.");
886 }
887 }
void Log(const char *format,...) SK_PRINTF_LIKE(1
Definition: TestRunner.cpp:137

◆ commitBackGesture()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.commitBackGesture ( )
inlinepackage

Invoke this from OnBackAnimationCallback#onBackInvoked().

This method is called to signify the completion of a back gesture and commits the navigation action initiated by the gesture. It should be invoked as the final step in handling a back gesture.

This method indicates to the Flutter framework that it should proceed with the back navigation, including finalizing animations and updating the UI to reflect the navigation outcome.

Definition at line 857 of file FlutterActivityAndFragmentDelegate.java.

857 {
858 ensureAlive();
859 if (flutterEngine != null) {
860 Log.v(TAG, "Forwarding commitBackGesture() to FlutterEngine.");
862 } else {
863 Log.w(TAG, "Invoked commitBackGesture() before FlutterFragment was attached to an Activity.");
864 }
865 }

◆ detachFromFlutterEngine()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.detachFromFlutterEngine ( )
inline

Definition at line 697 of file FlutterActivityAndFragmentDelegate.java.

697 {
698 if (host.shouldDestroyEngineWithHost()) {
699 // The host owns the engine and should never have its engine taken by another exclusive
700 // activity.
701 throw new AssertionError(
702 "The internal FlutterEngine created by "
703 + host
704 + " has been attached to by another activity. To persist a FlutterEngine beyond the "
705 + "ownership of this activity, explicitly create a FlutterEngine");
706 }
707
708 // Default, but customizable, behavior is for the host to call {@link #onDetach}
709 // deterministically as to not mix more events during the lifecycle of the next exclusive
710 // activity.
712 }

◆ getAppComponent()

Activity io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.getAppComponent ( )
inline

Definition at line 224 of file FlutterActivityAndFragmentDelegate.java.

224 {
225 final Activity activity = host.getActivity();
226 if (activity == null) {
227 throw new AssertionError(
228 "FlutterActivityAndFragmentDelegate's getAppComponent should only "
229 + "be queried after onAttach, when the host's activity should always be non-null");
230 }
231 return activity;
232 }

◆ getFlutterEngine()

FlutterEngine io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.getFlutterEngine ( )
inlinepackage

Returns the io.flutter.embedding.engine.FlutterEngine that is owned by this delegate and its host Activity or Fragment.

Definition at line 150 of file FlutterActivityAndFragmentDelegate.java.

150 {
151 return flutterEngine;
152 }

◆ isAttached()

boolean io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.isAttached ( )
inlinepackage

Whether or not this FlutterActivityAndFragmentDelegate is attached to a
FlutterEngine
.

Definition at line 166 of file FlutterActivityAndFragmentDelegate.java.

166 {
167 return isAttached;
168 }

◆ isFlutterEngineFromHost()

boolean io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.isFlutterEngineFromHost ( )
inlinepackage

Returns true if the host Activity/Fragment provided a FlutterEngine, as opposed to this delegate creating a new one.

Definition at line 158 of file FlutterActivityAndFragmentDelegate.java.

158 {
159 return isFlutterEngineFromHost;
160 }

◆ onActivityResult()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult ( int  requestCode,
int  resultCode,
Intent  data 
)
inlinepackage

Invoke this from Activity#onActivityResult(int, int, Intent) or
Fragment#onActivityResult(int, int, Intent)
.

This method forwards to interested Flutter plugins.

Definition at line 950 of file FlutterActivityAndFragmentDelegate.java.

950 {
951 ensureAlive();
952 if (flutterEngine != null) {
953 Log.v(
954 TAG,
955 "Forwarding onActivityResult() to FlutterEngine:\n"
956 + "requestCode: "
957 + requestCode
958 + "\n"
959 + "resultCode: "
960 + resultCode
961 + "\n"
962 + "data: "
963 + data);
964 flutterEngine.getActivityControlSurface().onActivityResult(requestCode, resultCode, data);
965 } else {
966 Log.w(TAG, "onActivityResult() invoked before FlutterFragment was attached to an Activity.");
967 }
968 }
ActivityControlSurface getActivityControlSurface()
boolean onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ onAttach()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach ( @NonNull Context  context)
inlinepackage

Invoke this method from Activity#onCreate(Bundle) or
Fragment#onAttach(Context)
.

This method does the following:

  1. Initializes the Flutter system.
  2. Obtains or creates a io.flutter.embedding.engine.FlutterEngine.
  3. Creates and configures a PlatformPlugin.
  4. Attaches the io.flutter.embedding.engine.FlutterEngine to the surrounding
    Activity
    , if desired.
  5. Configures the io.flutter.embedding.engine.FlutterEngine via Host#configureFlutterEngine(FlutterEngine).

Definition at line 188 of file FlutterActivityAndFragmentDelegate.java.

188 {
189 ensureAlive();
190
191 // When "retain instance" is true, the FlutterEngine will survive configuration
192 // changes. Therefore, we create a new one only if one does not already exist.
193 if (flutterEngine == null) {
195 }
196
197 if (host.shouldAttachEngineToActivity()) {
198 // Notify any plugins that are currently attached to our FlutterEngine that they
199 // are now attached to an Activity.
200 //
201 // Passing this Fragment's Lifecycle should be sufficient because as long as this Fragment
202 // is attached to its Activity, the lifecycles should be in sync. Once this Fragment is
203 // detached from its Activity, that Activity will be detached from the FlutterEngine, too,
204 // which means there shouldn't be any possibility for the Fragment Lifecycle to get out of
205 // sync with the Activity. We use the Fragment's Lifecycle because it is possible that the
206 // attached Activity is not a LifecycleOwner.
207 Log.v(TAG, "Attaching FlutterEngine to the Activity that owns this delegate.");
208 flutterEngine.getActivityControlSurface().attachToActivity(this, host.getLifecycle());
209 }
210
211 // Regardless of whether or not a FlutterEngine already existed, the PlatformPlugin
212 // is bound to a specific Activity. Therefore, it needs to be created and configured
213 // every time this Fragment attaches to a new Activity.
214 // TODO(mattcarroll): the PlatformPlugin needs to be reimagined because it implicitly takes
215 // control of the entire window. This is unacceptable for non-fullscreen
216 // use-cases.
217 platformPlugin = host.providePlatformPlugin(host.getActivity(), flutterEngine);
218
219 host.configureFlutterEngine(flutterEngine);
220 isAttached = true;
221 }
PlatformPlugin providePlatformPlugin( @Nullable Activity activity, @NonNull FlutterEngine flutterEngine)
void configureFlutterEngine(@NonNull FlutterEngine flutterEngine)
void attachToActivity( @NonNull ExclusiveAppComponent< Activity > exclusiveActivity, @NonNull Lifecycle lifecycle)

◆ onBackPressed()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onBackPressed ( )
inlinepackage

Invoke this from android.app.Activity#onBackPressed().

A Fragment host must have its containing Activity forward this call so that the Fragment can then invoke this method.

This method instructs Flutter's navigation system to "pop route".

Definition at line 785 of file FlutterActivityAndFragmentDelegate.java.

785 {
786 ensureAlive();
787 if (flutterEngine != null) {
788 Log.v(TAG, "Forwarding onBackPressed() to FlutterEngine.");
789 flutterEngine.getNavigationChannel().popRoute();
790 } else {
791 Log.w(TAG, "Invoked onBackPressed() before FlutterFragment was attached to an Activity.");
792 }
793 }

◆ onCreateView()

View io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onCreateView ( LayoutInflater  inflater,
@Nullable ViewGroup  container,
@Nullable Bundle  savedInstanceState,
int  flutterViewId,
boolean  shouldDelayFirstAndroidViewDraw 
)
inlinepackage

Invoke this method from Activity#onCreate(Bundle) to create the content View, or from Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle).

inflater and container may be null when invoked from an Activity.

shouldDelayFirstAndroidViewDraw determines whether to set up an android.view.ViewTreeObserver.OnPreDrawListener, which will defer the current drawing pass till after the Flutter UI has been displayed. This results in more accurate timings reported with Android tools, such as "Displayed" timing printed with am start.

Note that it should only be set to true when Host#getRenderMode() is
RenderMode.surface
.

This method:

  1. creates a new FlutterView in a View hierarchy
  2. adds a FlutterUiDisplayListener to it
  3. attaches a io.flutter.embedding.engine.FlutterEngine to the new FlutterView
  4. returns the new View hierarchy

Definition at line 365 of file FlutterActivityAndFragmentDelegate.java.

370 {
371 Log.v(TAG, "Creating FlutterView.");
372 ensureAlive();
373
374 if (host.getRenderMode() == RenderMode.surface) {
375 FlutterSurfaceView flutterSurfaceView =
376 new FlutterSurfaceView(
377 host.getContext(), host.getTransparencyMode() == TransparencyMode.transparent);
378
379 // Allow our host to customize FlutterSurfaceView, if desired.
380 host.onFlutterSurfaceViewCreated(flutterSurfaceView);
381
382 // Create the FlutterView that owns the FlutterSurfaceView.
383 flutterView = new FlutterView(host.getContext(), flutterSurfaceView);
384 } else {
385 FlutterTextureView flutterTextureView = new FlutterTextureView(host.getContext());
386
387 flutterTextureView.setOpaque(host.getTransparencyMode() == TransparencyMode.opaque);
388
389 // Allow our host to customize FlutterSurfaceView, if desired.
390 host.onFlutterTextureViewCreated(flutterTextureView);
391
392 // Create the FlutterView that owns the FlutterTextureView.
393 flutterView = new FlutterView(host.getContext(), flutterTextureView);
394 }
395
396 // Add listener to be notified when Flutter renders its first frame.
397 flutterView.addOnFirstFrameRenderedListener(flutterUiDisplayListener);
398
399 if (host.attachToEngineAutomatically()) {
400 Log.v(TAG, "Attaching FlutterEngine to FlutterView.");
401 flutterView.attachToFlutterEngine(flutterEngine);
402 }
403 flutterView.setId(flutterViewId);
404
405 if (shouldDelayFirstAndroidViewDraw) {
406 delayFirstAndroidViewDraw(flutterView);
407 }
408 return flutterView;
409 }
void addOnFirstFrameRenderedListener(@NonNull FlutterUiDisplayListener listener)
void attachToFlutterEngine(@NonNull FlutterEngine flutterEngine)
void onFlutterTextureViewCreated(@NonNull FlutterTextureView flutterTextureView)
void onFlutterSurfaceViewCreated(@NonNull FlutterSurfaceView flutterSurfaceView)

◆ onDestroyView()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onDestroyView ( )
inlinepackage

Invoke this from Activity#onDestroy() or Fragment#onDestroyView().

This method removes this delegate's FlutterView's FlutterUiDisplayListener.

Definition at line 662 of file FlutterActivityAndFragmentDelegate.java.

662 {
663 Log.v(TAG, "onDestroyView()");
664 ensureAlive();
665
666 if (activePreDrawListener != null) {
667 flutterView.getViewTreeObserver().removeOnPreDrawListener(activePreDrawListener);
669 }
670
671 // flutterView can be null in instances where a delegate.onDestroyView is called without
672 // onCreateView being called. See https://github.com/flutter/engine/pull/41082 for more detail.
673 if (flutterView != null) {
675 flutterView.removeOnFirstFrameRenderedListener(flutterUiDisplayListener);
676 }
677 }
void removeOnFirstFrameRenderedListener(@NonNull FlutterUiDisplayListener listener)

◆ onDetach()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onDetach ( )
inlinepackage

Invoke this from Activity#onDestroy() or Fragment#onDetach().

This method:

  1. Detaches this delegate's io.flutter.embedding.engine.FlutterEngine from its surrounding Activity, if it was previously attached.
  2. Destroys this delegate's PlatformPlugin.
  3. Destroys this delegate's io.flutter.embedding.engine.FlutterEngine if Host#shouldDestroyEngineWithHost() ()} returns true.

Definition at line 729 of file FlutterActivityAndFragmentDelegate.java.

729 {
730 if (!isAttached) {
731 // Already detached.
732 return;
733 }
734 Log.v(TAG, "onDetach()");
735 ensureAlive();
736
737 // Give the host an opportunity to cleanup any references that were created in
738 // configureFlutterEngine().
739 host.cleanUpFlutterEngine(flutterEngine);
740
741 if (host.shouldAttachEngineToActivity()) {
742 // Notify plugins that they are no longer attached to an Activity.
743 Log.v(TAG, "Detaching FlutterEngine from the Activity that owns this Fragment.");
744 if (host.getActivity().isChangingConfigurations()) {
746 } else {
748 }
749 }
750
751 // Null out the platformPlugin to avoid a possible retain cycle between the plugin, this
752 // Fragment,
753 // and this Fragment's Activity.
754 if (platformPlugin != null) {
755 platformPlugin.destroy();
756 platformPlugin = null;
757 }
758
759 if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
760 flutterEngine.getLifecycleChannel().appIsDetached();
761 }
762
763 // Destroy our FlutterEngine if we're not set to retain it.
764 if (host.shouldDestroyEngineWithHost()) {
765 flutterEngine.destroy();
766
767 if (host.getCachedEngineId() != null) {
768 FlutterEngineCache.getInstance().remove(host.getCachedEngineId());
769 }
770
771 flutterEngine = null;
772 }
773
774 isAttached = false;
775 }
void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine)

◆ onNewIntent()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onNewIntent ( @NonNull Intent  intent)
inlinepackage

Invoke this from Activity#onNewIntent(Intent).

A Fragment host must have its containing Activity forward this call so that the Fragment can then invoke this method.

This method forwards to interested Flutter plugins.

Definition at line 928 of file FlutterActivityAndFragmentDelegate.java.

928 {
929 ensureAlive();
930 if (flutterEngine != null) {
931 Log.v(
932 TAG,
933 "Forwarding onNewIntent() to FlutterEngine and sending pushRouteInformation message.");
934 flutterEngine.getActivityControlSurface().onNewIntent(intent);
935 String initialRoute = maybeGetInitialRouteFromIntent(intent);
936 if (initialRoute != null && !initialRoute.isEmpty()) {
937 flutterEngine.getNavigationChannel().pushRouteInformation(initialRoute);
938 }
939 } else {
940 Log.w(TAG, "onNewIntent() invoked before FlutterFragment was attached to an Activity.");
941 }
942 }

◆ onPause()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onPause ( )
inlinepackage

Invoke this from Activity#onPause() or Fragment#onPause().

This method notifies the running Flutter app that it is "inactive" as per the Flutter app lifecycle.

Definition at line 612 of file FlutterActivityAndFragmentDelegate.java.

612 {
613 Log.v(TAG, "onPause()");
614 ensureAlive();
615 if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
616 flutterEngine.getLifecycleChannel().appIsInactive();
617 }
618 }

◆ onPostResume()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onPostResume ( )
inlinepackage

Invoke this from Activity#onPostResume().

A Fragment host must have its containing Activity forward this call so that the Fragment can then invoke this method.

This method informs the PlatformPlugin that onPostResume() has run, which is used to update system UI overlays.

Definition at line 582 of file FlutterActivityAndFragmentDelegate.java.

582 {
583 Log.v(TAG, "onPostResume()");
584 ensureAlive();
585 if (flutterEngine != null) {
587 flutterEngine.getPlatformViewsController().onResume();
588 } else {
589 Log.w(TAG, "onPostResume() invoked before FlutterFragment was attached to an Activity.");
590 }
591 }
PlatformViewsController getPlatformViewsController()

◆ onRequestPermissionsResult()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onRequestPermissionsResult ( int  requestCode,
@NonNull String[]  permissions,
@NonNull int[]  grantResults 
)
inlinepackage

Invoke this from android.app.Activity#onRequestPermissionsResult(int, String[], int[]) or Fragment#onRequestPermissionsResult(int, String[], int[]).

This method forwards to interested Flutter plugins.

Definition at line 895 of file FlutterActivityAndFragmentDelegate.java.

896 {
897 ensureAlive();
898 if (flutterEngine != null) {
899 Log.v(
900 TAG,
901 "Forwarding onRequestPermissionsResult() to FlutterEngine:\n"
902 + "requestCode: "
903 + requestCode
904 + "\n"
905 + "permissions: "
906 + Arrays.toString(permissions)
907 + "\n"
908 + "grantResults: "
909 + Arrays.toString(grantResults));
910 flutterEngine
912 .onRequestPermissionsResult(requestCode, permissions, grantResults);
913 } else {
914 Log.w(
915 TAG,
916 "onRequestPermissionResult() invoked before FlutterFragment was attached to an Activity.");
917 }
918 }
boolean onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResult)

◆ onRestoreInstanceState()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onRestoreInstanceState ( @Nullable Bundle  bundle)
inlinepackage

Definition at line 411 of file FlutterActivityAndFragmentDelegate.java.

411 {
412 Log.v(
413 TAG,
414 "onRestoreInstanceState. Giving framework and plugins an opportunity to restore state.");
415 ensureAlive();
416
417 Bundle pluginState = null;
418 byte[] frameworkState = null;
419 if (bundle != null) {
420 pluginState = bundle.getBundle(PLUGINS_RESTORATION_BUNDLE_KEY);
421 frameworkState = bundle.getByteArray(FRAMEWORK_RESTORATION_BUNDLE_KEY);
422 }
423
424 if (host.shouldRestoreAndSaveState()) {
425 flutterEngine.getRestorationChannel().setRestorationData(frameworkState);
426 }
427
428 if (host.shouldAttachEngineToActivity()) {
429 flutterEngine.getActivityControlSurface().onRestoreInstanceState(pluginState);
430 }
431 }

◆ onResume()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onResume ( )
inlinepackage

Invoke this from Activity#onResume() or Fragment#onResume().

This method notifies the running Flutter app that it is "resumed" as per the Flutter app lifecycle.

Definition at line 563 of file FlutterActivityAndFragmentDelegate.java.

563 {
564 Log.v(TAG, "onResume()");
565 ensureAlive();
566 if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
567 flutterEngine.getLifecycleChannel().appIsResumed();
568 }
569 }

◆ onSaveInstanceState()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onSaveInstanceState ( @Nullable Bundle  bundle)
inlinepackage

Definition at line 679 of file FlutterActivityAndFragmentDelegate.java.

679 {
680 Log.v(TAG, "onSaveInstanceState. Giving framework and plugins an opportunity to save state.");
681 ensureAlive();
682
683 if (host.shouldRestoreAndSaveState()) {
684 bundle.putByteArray(
685 FRAMEWORK_RESTORATION_BUNDLE_KEY,
686 flutterEngine.getRestorationChannel().getRestorationData());
687 }
688
689 if (host.shouldAttachEngineToActivity()) {
690 final Bundle plugins = new Bundle();
691 flutterEngine.getActivityControlSurface().onSaveInstanceState(plugins);
692 bundle.putBundle(PLUGINS_RESTORATION_BUNDLE_KEY, plugins);
693 }
694 }

◆ onStart()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onStart ( )
inlinepackage

Invoke this from Activity#onStart() or Fragment#onStart().

This method:

  1. Begins executing Dart code, if it is not already executing.

Definition at line 444 of file FlutterActivityAndFragmentDelegate.java.

444 {
445 Log.v(TAG, "onStart()");
446 ensureAlive();
447 doInitialFlutterViewRun();
448 // This is a workaround for a bug on some OnePlus phones. The visibility of the application
449 // window is still true after locking the screen on some OnePlus phones, and shows a black
450 // screen when unlocked. We can work around this by changing the visibility of FlutterView in
451 // onStart and onStop.
452 // See https://github.com/flutter/flutter/issues/93276
453 if (previousVisibility != null) {
454 flutterView.setVisibility(previousVisibility);
455 }
456 }

◆ onStop()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onStop ( )
inlinepackage

Invoke this from Activity#onStop() or Fragment#onStop().

This method:

  1. This method notifies the running Flutter app that it is "paused" as per the Flutter app lifecycle.
  2. Detaches this delegate's io.flutter.embedding.engine.FlutterEngine from this delegate's FlutterView.

Definition at line 634 of file FlutterActivityAndFragmentDelegate.java.

634 {
635 Log.v(TAG, "onStop()");
636 ensureAlive();
637
638 if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
639 flutterEngine.getLifecycleChannel().appIsPaused();
640 }
641
642 // This is a workaround for a bug on some OnePlus phones. The visibility of the application
643 // window is still true after locking the screen on some OnePlus phones, and shows a black
644 // screen when unlocked. We can work around this by changing the visibility of FlutterView in
645 // onStart and onStop.
646 // See https://github.com/flutter/flutter/issues/93276
647 previousVisibility = flutterView.getVisibility();
648 flutterView.setVisibility(View.GONE);
649 if (flutterEngine != null) {
650 // When an Activity is stopped it won't have its onTrimMemory callback invoked. Normally,
651 // this isn't a problem but because of a bug in some builds of Android 14 we must act as
652 // if the onTrimMemory callback has been called.
653 flutterEngine.getRenderer().onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_BACKGROUND);
654 }
655 }

◆ onTrimMemory()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onTrimMemory ( int  level)
inlinepackage

Invoke this from android.app.Activity#onTrimMemory(int).

A Fragment host must have its containing Activity forward this call so that the Fragment can then invoke this method.

This method sends a "memory pressure warning" message to Flutter over the "system channel".

Definition at line 1017 of file FlutterActivityAndFragmentDelegate.java.

1017 {
1018 ensureAlive();
1019 if (flutterEngine != null) {
1020 // Use a trim level delivered while the application is running so the
1021 // framework has a chance to react to the notification.
1022 // Avoid being too aggressive before the first frame is rendered. If it is
1023 // not at least running critical, we should avoid delaying the frame for
1024 // an overly aggressive GC.
1025 boolean trim = isFirstFrameRendered && level >= TRIM_MEMORY_RUNNING_LOW;
1026 if (trim) {
1027 flutterEngine.getDartExecutor().notifyLowMemoryWarning();
1029 }
1030 flutterEngine.getRenderer().onTrimMemory(level);
1032 }
1033 }
static SkCanvas * trim(SkCanvas *canvas, SkScalar width, SkScalar height, const SkRect *content)
Definition: SkDocument.cpp:19

◆ onUserLeaveHint()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onUserLeaveHint ( )
inlinepackage

Invoke this from Activity#onUserLeaveHint().

A Fragment host must have its containing Activity forward this call so that the Fragment can then invoke this method.

This method forwards to interested Flutter plugins.

Definition at line 978 of file FlutterActivityAndFragmentDelegate.java.

978 {
979 ensureAlive();
980 if (flutterEngine != null) {
981 Log.v(TAG, "Forwarding onUserLeaveHint() to FlutterEngine.");
983 } else {
984 Log.w(TAG, "onUserLeaveHint() invoked before FlutterFragment was attached to an Activity.");
985 }
986 }

◆ onWindowFocusChanged()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onWindowFocusChanged ( boolean  hasFocus)
inlinepackage

Invoke this from Activity#onWindowFocusChanged().

A Fragment host must have its containing Activity forward this call so that the Fragment can then invoke this method.

Definition at line 994 of file FlutterActivityAndFragmentDelegate.java.

994 {
995 ensureAlive();
996 Log.v(TAG, "Received onWindowFocusChanged: " + (hasFocus ? "true" : "false"));
997 if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
998 // TODO(gspencergoog): Once we have support for multiple windows/views,
999 // this code will need to consult the list of windows/views to determine if
1000 // any windows in the app are focused and call the appropriate function.
1001 if (hasFocus) {
1002 flutterEngine.getLifecycleChannel().aWindowIsFocused();
1003 } else {
1004 flutterEngine.getLifecycleChannel().noWindowsAreFocused();
1005 }
1006 }
1007 }

◆ release()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.release ( )
inlinepackage

Disconnects this FlutterActivityAndFragmentDelegate from its host Activity or Fragment.

No further method invocations may occur on this FlutterActivityAndFragmentDelegate after invoking this method. If a method is invoked, an exception will occur.

This method only clears out references. It does not destroy its io.flutter.embedding.engine.FlutterEngine. The behavior that destroys a io.flutter.embedding.engine.FlutterEngine can be found in onDetach().

Definition at line 138 of file FlutterActivityAndFragmentDelegate.java.

138 {
139 this.host = null;
140 this.flutterEngine = null;
141 this.flutterView = null;
142 this.platformPlugin = null;
143 }

◆ setUpFlutterEngine()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.setUpFlutterEngine ( )
inlinepackage

Obtains a reference to a FlutterEngine to back this delegate and its host.

First, the host is asked if it would like to use a cached io.flutter.embedding.engine.FlutterEngine, and if so, the cached io.flutter.embedding.engine.FlutterEngine is retrieved.

Second, the host is given an opportunity to provide a io.flutter.embedding.engine.FlutterEngine via Host#provideFlutterEngine(Context).

Third, the host is asked if it would like to use a cached io.flutter.embedding.engine.FlutterEngineGroup to create a new FlutterEngine by FlutterEngineGroup#createAndRunEngine

If the host does not provide a io.flutter.embedding.engine.FlutterEngine, then a new FlutterEngine is instantiated.

Definition at line 276 of file FlutterActivityAndFragmentDelegate.java.

276 {
277 Log.v(TAG, "Setting up FlutterEngine.");
278
279 // First, check if the host wants to use a cached FlutterEngine.
280 String cachedEngineId = host.getCachedEngineId();
281 if (cachedEngineId != null) {
282 flutterEngine = FlutterEngineCache.getInstance().get(cachedEngineId);
283 isFlutterEngineFromHost = true;
284 if (flutterEngine == null) {
285 throw new IllegalStateException(
286 "The requested cached FlutterEngine did not exist in the FlutterEngineCache: '"
287 + cachedEngineId
288 + "'");
289 }
290 return;
291 }
292
293 // Second, defer to subclasses for a custom FlutterEngine.
294 flutterEngine = host.provideFlutterEngine(host.getContext());
295 if (flutterEngine != null) {
296 isFlutterEngineFromHost = true;
297 return;
298 }
299
300 // Third, check if the host wants to use a cached FlutterEngineGroup
301 // and create new FlutterEngine using FlutterEngineGroup#createAndRunEngine
302 String cachedEngineGroupId = host.getCachedEngineGroupId();
303 if (cachedEngineGroupId != null) {
304 FlutterEngineGroup flutterEngineGroup =
305 FlutterEngineGroupCache.getInstance().get(cachedEngineGroupId);
306 if (flutterEngineGroup == null) {
307 throw new IllegalStateException(
308 "The requested cached FlutterEngineGroup did not exist in the FlutterEngineGroupCache: '"
309 + cachedEngineGroupId
310 + "'");
311 }
312
313 flutterEngine =
314 flutterEngineGroup.createAndRunEngine(
315 addEntrypointOptions(new FlutterEngineGroup.Options(host.getContext())));
316 isFlutterEngineFromHost = false;
317 return;
318 }
319
320 // Our host did not provide a custom FlutterEngine. Create a FlutterEngine to back our
321 // FlutterView.
322 Log.v(
323 TAG,
324 "No preferred FlutterEngine was provided. Creating a new FlutterEngine for"
325 + " this FlutterFragment.");
326
328 engineGroup == null
330 : engineGroup;
331 flutterEngine =
332 group.createAndRunEngine(
333 addEntrypointOptions(
334 new FlutterEngineGroup.Options(host.getContext())
335 .setAutomaticallyRegisterPlugins(false)
336 .setWaitForRestorationData(host.shouldRestoreAndSaveState())));
337 isFlutterEngineFromHost = false;
338 }
FlutterEngine provideFlutterEngine(@NonNull Context context)

◆ startBackGesture()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.startBackGesture ( @NonNull BackEvent  backEvent)
inlinepackage

Invoke this from OnBackAnimationCallback#onBackStarted(BackEvent).

This method should be called when the back gesture is initiated. It should be invoked as part of the implementation of OnBackAnimationCallback.

This method delegates the handling of the start of a back gesture to the Flutter framework, which is responsible for the appropriate response, such as initiating animations or preparing the UI for the back navigation process.

Parameters
backEventThe BackEvent object containing information about the touch.

Definition at line 809 of file FlutterActivityAndFragmentDelegate.java.

809 {
810 ensureAlive();
811 if (flutterEngine != null) {
812 Log.v(TAG, "Forwarding startBackGesture() to FlutterEngine.");
813 flutterEngine.getBackGestureChannel().startBackGesture(backEvent);
814 } else {
815 Log.w(TAG, "Invoked startBackGesture() before FlutterFragment was attached to an Activity.");
816 }
817 }

◆ updateBackGestureProgress()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.updateBackGestureProgress ( @NonNull BackEvent  backEvent)
inlinepackage

Invoke this from OnBackAnimationCallback#onBackProgressed(BackEvent).

This method should be called in response to progress in a back gesture, as part of the implementation of OnBackAnimationCallback.

This method delegates to the Flutter framework to update UI elements or animations based on the progression of the back gesture.

Parameters
backEventAn BackEvent object describing the progress event.

Definition at line 832 of file FlutterActivityAndFragmentDelegate.java.

832 {
833 ensureAlive();
834 if (flutterEngine != null) {
835 Log.v(TAG, "Forwarding updateBackGestureProgress() to FlutterEngine.");
836 flutterEngine.getBackGestureChannel().updateBackGestureProgress(backEvent);
837 } else {
838 Log.w(
839 TAG,
840 "Invoked updateBackGestureProgress() before FlutterFragment was attached to an Activity.");
841 }
842 }

◆ updateSystemUiOverlays()

void io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.updateSystemUiOverlays ( )
inlinepackage

Refreshes Android's window system UI (AKA system chrome) to match Flutter's desired system chrome style.

Definition at line 597 of file FlutterActivityAndFragmentDelegate.java.

597 {
598 if (platformPlugin != null) {
599 // TODO(mattcarroll): find a better way to handle the update of UI overlays than calling
600 // through to platformPlugin. We're implicitly entangling the Window, Activity,
601 // Fragment, and engine all with this one call.
602 platformPlugin.updateSystemUiOverlays();
603 }
604 }

Member Data Documentation

◆ activePreDrawListener

OnPreDrawListener io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.activePreDrawListener
package

Definition at line 92 of file FlutterActivityAndFragmentDelegate.java.

◆ flutterView

FlutterView io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.flutterView
package

Definition at line 90 of file FlutterActivityAndFragmentDelegate.java.


The documentation for this class was generated from the following file: