From a200c803baefdae3b8ba2aa8466e2f3af9e8b7f1 Mon Sep 17 00:00:00 2001 From: Ajmal Kunnummal Date: Mon, 12 Jul 2021 10:40:10 -0700 Subject: [PATCH 1/2] Make FlutterFragment usable without requiring it to be attached to an Android Activity. - call getContext instead of getActivity in FlutterActivityAndFragmentDelegate.onCreateView --- .../android/FlutterActivityAndFragmentDelegate.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterActivityAndFragmentDelegate.java b/shell/platform/android/io/flutter/embedding/android/FlutterActivityAndFragmentDelegate.java index a789a7a95a82b..9f0dc968bec83 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterActivityAndFragmentDelegate.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterActivityAndFragmentDelegate.java @@ -273,15 +273,15 @@ View onCreateView( if (host.getRenderMode() == RenderMode.surface) { FlutterSurfaceView flutterSurfaceView = new FlutterSurfaceView( - host.getActivity(), host.getTransparencyMode() == TransparencyMode.transparent); + host.getContext(), host.getTransparencyMode() == TransparencyMode.transparent); // Allow our host to customize FlutterSurfaceView, if desired. host.onFlutterSurfaceViewCreated(flutterSurfaceView); // Create the FlutterView that owns the FlutterSurfaceView. - flutterView = new FlutterView(host.getActivity(), flutterSurfaceView); + flutterView = new FlutterView(host.getContext(), flutterSurfaceView); } else { - FlutterTextureView flutterTextureView = new FlutterTextureView(host.getActivity()); + FlutterTextureView flutterTextureView = new FlutterTextureView(host.getContext()); flutterTextureView.setOpaque(host.getTransparencyMode() == TransparencyMode.opaque); @@ -289,7 +289,7 @@ View onCreateView( host.onFlutterTextureViewCreated(flutterTextureView); // Create the FlutterView that owns the FlutterTextureView. - flutterView = new FlutterView(host.getActivity(), flutterTextureView); + flutterView = new FlutterView(host.getContext(), flutterTextureView); } // Add listener to be notified when Flutter renders its first frame. From ca4bbbccfef1e826a5cff6b73b5b2d00a9eda6e8 Mon Sep 17 00:00:00 2001 From: Ajmal Kunnummal Date: Tue, 13 Jul 2021 16:06:50 -0700 Subject: [PATCH 2/2] Make sure tests fully cover the case where there is no attached activity. --- ...FlutterActivityAndFragmentDelegateTest.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java b/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java index 8dbe0ba8dfb58..07b2608c9463c 100644 --- a/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java +++ b/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java @@ -378,6 +378,9 @@ public void itDoesNotAttachFlutterToTheActivityIfNotDesired() { // Declare that the host does NOT want Flutter to attach to the surrounding Activity. when(mockHost.shouldAttachEngineToActivity()).thenReturn(false); + // getActivity() returns null if the activity is not attached + when(mockHost.getActivity()).thenReturn(null); + // Create the real object that we're testing. FlutterActivityAndFragmentDelegate delegate = new FlutterActivityAndFragmentDelegate(mockHost); @@ -385,14 +388,21 @@ public void itDoesNotAttachFlutterToTheActivityIfNotDesired() { // Flutter is attached to the surrounding Activity in onAttach. delegate.onAttach(RuntimeEnvironment.application); - // Verify that the ActivityControlSurface was NOT told to attach to an Activity. - verify(mockFlutterEngine.getActivityControlSurface(), never()) - .attachToActivity(any(Activity.class), any(Lifecycle.class)); + // Make sure all of the other lifecycle methods can run safely as well + // without a valid Activity + delegate.onCreateView(null, null, null); + delegate.onStart(); + delegate.onResume(); + delegate.onPause(); + delegate.onStop(); + delegate.onDestroyView(); // Flutter is detached from the surrounding Activity in onDetach. delegate.onDetach(); - // Verify that the ActivityControlSurface was NOT told to detach from the Activity. + // Verify that the ActivityControlSurface was NOT told to attach or detach to an Activity. + verify(mockFlutterEngine.getActivityControlSurface(), never()) + .attachToActivity(any(Activity.class), any(Lifecycle.class)); verify(mockFlutterEngine.getActivityControlSurface(), never()).detachFromActivity(); }