Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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 74 of file FlutterActivityAndFragmentDelegate.java.

Constructor & Destructor Documentation

◆ FlutterActivityAndFragmentDelegate() [1/2]

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

Definition at line 116 of file FlutterActivityAndFragmentDelegate.java.

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

◆ FlutterActivityAndFragmentDelegate() [2/2]

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

Definition at line 120 of file FlutterActivityAndFragmentDelegate.java.

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

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 871 of file FlutterActivityAndFragmentDelegate.java.

871 {
872 ensureAlive();
873 if (flutterEngine != null) {
874 Log.v(TAG, "Forwarding cancelBackGesture() to FlutterEngine.");
875 flutterEngine.getBackGestureChannel().cancelBackGesture();
876 } else {
877 Log.w(TAG, "Invoked cancelBackGesture() before FlutterFragment was attached to an Activity.");
878 }
879 }
void Log(const char *format,...) SK_PRINTF_LIKE(1

◆ 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 849 of file FlutterActivityAndFragmentDelegate.java.

849 {
850 ensureAlive();
851 if (flutterEngine != null) {
852 Log.v(TAG, "Forwarding commitBackGesture() to FlutterEngine.");
853 flutterEngine.getBackGestureChannel().commitBackGesture();
854 } else {
855 Log.w(TAG, "Invoked commitBackGesture() before FlutterFragment was attached to an Activity.");
856 }
857 }

◆ detachFromFlutterEngine()

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

Definition at line 689 of file FlutterActivityAndFragmentDelegate.java.

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

◆ getAppComponent()

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

Definition at line 223 of file FlutterActivityAndFragmentDelegate.java.

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

◆ 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 149 of file FlutterActivityAndFragmentDelegate.java.

149 {
150 return flutterEngine;
151 }

◆ isAttached()

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

Whether or not this FlutterActivityAndFragmentDelegate is attached to a
FlutterEngine
.

Definition at line 165 of file FlutterActivityAndFragmentDelegate.java.

165 {
166 return isAttached;
167 }

◆ 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 157 of file FlutterActivityAndFragmentDelegate.java.

157 {
158 return isFlutterEngineFromHost;
159 }

◆ 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 942 of file FlutterActivityAndFragmentDelegate.java.

942 {
943 ensureAlive();
944 if (flutterEngine != null) {
945 Log.v(
946 TAG,
947 "Forwarding onActivityResult() to FlutterEngine:\n"
948 + "requestCode: "
949 + requestCode
950 + "\n"
951 + "resultCode: "
952 + resultCode
953 + "\n"
954 + "data: "
955 + data);
956 flutterEngine.getActivityControlSurface().onActivityResult(requestCode, resultCode, data);
957 } else {
958 Log.w(TAG, "onActivityResult() invoked before FlutterFragment was attached to an Activity.");
959 }
960 }

◆ 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 187 of file FlutterActivityAndFragmentDelegate.java.

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

◆ 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 777 of file FlutterActivityAndFragmentDelegate.java.

777 {
778 ensureAlive();
779 if (flutterEngine != null) {
780 Log.v(TAG, "Forwarding onBackPressed() to FlutterEngine.");
781 flutterEngine.getNavigationChannel().popRoute();
782 } else {
783 Log.w(TAG, "Invoked onBackPressed() before FlutterFragment was attached to an Activity.");
784 }
785 }

◆ 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 364 of file FlutterActivityAndFragmentDelegate.java.

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

654 {
655 Log.v(TAG, "onDestroyView()");
656 ensureAlive();
657
658 if (activePreDrawListener != null) {
659 flutterView.getViewTreeObserver().removeOnPreDrawListener(activePreDrawListener);
661 }
662
663 // flutterView can be null in instances where a delegate.onDestroyView is called without
664 // onCreateView being called. See https://github.com/flutter/engine/pull/41082 for more detail.
665 if (flutterView != null) {
667 flutterView.removeOnFirstFrameRenderedListener(flutterUiDisplayListener);
668 }
669 }
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 721 of file FlutterActivityAndFragmentDelegate.java.

721 {
722 if (!isAttached) {
723 // Already detached.
724 return;
725 }
726 Log.v(TAG, "onDetach()");
727 ensureAlive();
728
729 // Give the host an opportunity to cleanup any references that were created in
730 // configureFlutterEngine().
731 host.cleanUpFlutterEngine(flutterEngine);
732
733 if (host.shouldAttachEngineToActivity()) {
734 // Notify plugins that they are no longer attached to an Activity.
735 Log.v(TAG, "Detaching FlutterEngine from the Activity that owns this Fragment.");
736 if (host.getActivity().isChangingConfigurations()) {
737 flutterEngine.getActivityControlSurface().detachFromActivityForConfigChanges();
738 } else {
739 flutterEngine.getActivityControlSurface().detachFromActivity();
740 }
741 }
742
743 // Null out the platformPlugin to avoid a possible retain cycle between the plugin, this
744 // Fragment,
745 // and this Fragment's Activity.
746 if (platformPlugin != null) {
747 platformPlugin.destroy();
748 platformPlugin = null;
749 }
750
751 if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
752 flutterEngine.getLifecycleChannel().appIsDetached();
753 }
754
755 // Destroy our FlutterEngine if we're not set to retain it.
756 if (host.shouldDestroyEngineWithHost()) {
757 flutterEngine.destroy();
758
759 if (host.getCachedEngineId() != null) {
760 FlutterEngineCache.getInstance().remove(host.getCachedEngineId());
761 }
762
763 flutterEngine = null;
764 }
765
766 isAttached = false;
767 }
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 920 of file FlutterActivityAndFragmentDelegate.java.

920 {
921 ensureAlive();
922 if (flutterEngine != null) {
923 Log.v(
924 TAG,
925 "Forwarding onNewIntent() to FlutterEngine and sending pushRouteInformation message.");
926 flutterEngine.getActivityControlSurface().onNewIntent(intent);
927 String initialRoute = maybeGetInitialRouteFromIntent(intent);
928 if (initialRoute != null && !initialRoute.isEmpty()) {
929 flutterEngine.getNavigationChannel().pushRouteInformation(initialRoute);
930 }
931 } else {
932 Log.w(TAG, "onNewIntent() invoked before FlutterFragment was attached to an Activity.");
933 }
934 }

◆ 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 610 of file FlutterActivityAndFragmentDelegate.java.

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

◆ 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 581 of file FlutterActivityAndFragmentDelegate.java.

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

◆ 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 887 of file FlutterActivityAndFragmentDelegate.java.

888 {
889 ensureAlive();
890 if (flutterEngine != null) {
891 Log.v(
892 TAG,
893 "Forwarding onRequestPermissionsResult() to FlutterEngine:\n"
894 + "requestCode: "
895 + requestCode
896 + "\n"
897 + "permissions: "
898 + Arrays.toString(permissions)
899 + "\n"
900 + "grantResults: "
901 + Arrays.toString(grantResults));
902 flutterEngine
903 .getActivityControlSurface()
904 .onRequestPermissionsResult(requestCode, permissions, grantResults);
905 } else {
906 Log.w(
907 TAG,
908 "onRequestPermissionResult() invoked before FlutterFragment was attached to an Activity.");
909 }
910 }

◆ onRestoreInstanceState()

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

Definition at line 410 of file FlutterActivityAndFragmentDelegate.java.

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

◆ 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 562 of file FlutterActivityAndFragmentDelegate.java.

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

◆ onSaveInstanceState()

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

Definition at line 671 of file FlutterActivityAndFragmentDelegate.java.

671 {
672 Log.v(TAG, "onSaveInstanceState. Giving framework and plugins an opportunity to save state.");
673 ensureAlive();
674
675 if (host.shouldRestoreAndSaveState()) {
676 bundle.putByteArray(
677 FRAMEWORK_RESTORATION_BUNDLE_KEY,
678 flutterEngine.getRestorationChannel().getRestorationData());
679 }
680
681 if (host.shouldAttachEngineToActivity()) {
682 final Bundle plugins = new Bundle();
683 flutterEngine.getActivityControlSurface().onSaveInstanceState(plugins);
684 bundle.putBundle(PLUGINS_RESTORATION_BUNDLE_KEY, plugins);
685 }
686 }

◆ 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 443 of file FlutterActivityAndFragmentDelegate.java.

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

◆ 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 632 of file FlutterActivityAndFragmentDelegate.java.

632 {
633 Log.v(TAG, "onStop()");
634 ensureAlive();
635
636 if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
637 flutterEngine.getLifecycleChannel().appIsPaused();
638 }
639
640 // This is a workaround for a bug on some OnePlus phones. The visibility of the application
641 // window is still true after locking the screen on some OnePlus phones, and shows a black
642 // screen when unlocked. We can work around this by changing the visibility of FlutterView in
643 // onStart and onStop.
644 // See https://github.com/flutter/flutter/issues/93276
645 previousVisibility = flutterView.getVisibility();
646 flutterView.setVisibility(View.GONE);
647 }

◆ 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 1009 of file FlutterActivityAndFragmentDelegate.java.

1009 {
1010 ensureAlive();
1011 if (flutterEngine != null) {
1012 // Use a trim level delivered while the application is running so the
1013 // framework has a chance to react to the notification.
1014 // Avoid being too aggressive before the first frame is rendered. If it is
1015 // not at least running critical, we should avoid delaying the frame for
1016 // an overly aggressive GC.
1017 boolean trim = isFirstFrameRendered && level >= TRIM_MEMORY_RUNNING_LOW;
1018 if (trim) {
1019 flutterEngine.getDartExecutor().notifyLowMemoryWarning();
1020 flutterEngine.getSystemChannel().sendMemoryPressureWarning();
1021 }
1022 flutterEngine.getRenderer().onTrimMemory(level);
1023 }
1024 }
static SkCanvas * trim(SkCanvas *canvas, SkScalar width, SkScalar height, const SkRect *content)

◆ 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 970 of file FlutterActivityAndFragmentDelegate.java.

970 {
971 ensureAlive();
972 if (flutterEngine != null) {
973 Log.v(TAG, "Forwarding onUserLeaveHint() to FlutterEngine.");
974 flutterEngine.getActivityControlSurface().onUserLeaveHint();
975 } else {
976 Log.w(TAG, "onUserLeaveHint() invoked before FlutterFragment was attached to an Activity.");
977 }
978 }

◆ 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 986 of file FlutterActivityAndFragmentDelegate.java.

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

◆ 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 137 of file FlutterActivityAndFragmentDelegate.java.

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

◆ 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 275 of file FlutterActivityAndFragmentDelegate.java.

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

801 {
802 ensureAlive();
803 if (flutterEngine != null) {
804 Log.v(TAG, "Forwarding startBackGesture() to FlutterEngine.");
805 flutterEngine.getBackGestureChannel().startBackGesture(backEvent);
806 } else {
807 Log.w(TAG, "Invoked startBackGesture() before FlutterFragment was attached to an Activity.");
808 }
809 }

◆ 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 824 of file FlutterActivityAndFragmentDelegate.java.

824 {
825 ensureAlive();
826 if (flutterEngine != null) {
827 Log.v(TAG, "Forwarding updateBackGestureProgress() to FlutterEngine.");
828 flutterEngine.getBackGestureChannel().updateBackGestureProgress(backEvent);
829 } else {
830 Log.w(
831 TAG,
832 "Invoked updateBackGestureProgress() before FlutterFragment was attached to an Activity.");
833 }
834 }

◆ 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 595 of file FlutterActivityAndFragmentDelegate.java.

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

Member Data Documentation

◆ activePreDrawListener

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

Definition at line 91 of file FlutterActivityAndFragmentDelegate.java.

◆ flutterView

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

Definition at line 89 of file FlutterActivityAndFragmentDelegate.java.


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