Skip to content

Commit 317166d

Browse files
authored
Make FlutterFragment usable without requiring it to be attached to an Android Activity. (flutter#27332)
1 parent 639e52f commit 317166d

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

shell/platform/android/io/flutter/embedding/android/FlutterActivityAndFragmentDelegate.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,23 +276,23 @@ View onCreateView(
276276
if (host.getRenderMode() == RenderMode.surface) {
277277
FlutterSurfaceView flutterSurfaceView =
278278
new FlutterSurfaceView(
279-
host.getActivity(), host.getTransparencyMode() == TransparencyMode.transparent);
279+
host.getContext(), host.getTransparencyMode() == TransparencyMode.transparent);
280280

281281
// Allow our host to customize FlutterSurfaceView, if desired.
282282
host.onFlutterSurfaceViewCreated(flutterSurfaceView);
283283

284284
// Create the FlutterView that owns the FlutterSurfaceView.
285-
flutterView = new FlutterView(host.getActivity(), flutterSurfaceView);
285+
flutterView = new FlutterView(host.getContext(), flutterSurfaceView);
286286
} else {
287-
FlutterTextureView flutterTextureView = new FlutterTextureView(host.getActivity());
287+
FlutterTextureView flutterTextureView = new FlutterTextureView(host.getContext());
288288

289289
flutterTextureView.setOpaque(host.getTransparencyMode() == TransparencyMode.opaque);
290290

291291
// Allow our host to customize FlutterSurfaceView, if desired.
292292
host.onFlutterTextureViewCreated(flutterTextureView);
293293

294294
// Create the FlutterView that owns the FlutterTextureView.
295-
flutterView = new FlutterView(host.getActivity(), flutterTextureView);
295+
flutterView = new FlutterView(host.getContext(), flutterTextureView);
296296
}
297297

298298
// Add listener to be notified when Flutter renders its first frame.

shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,21 +378,31 @@ public void itDoesNotAttachFlutterToTheActivityIfNotDesired() {
378378
// Declare that the host does NOT want Flutter to attach to the surrounding Activity.
379379
when(mockHost.shouldAttachEngineToActivity()).thenReturn(false);
380380

381+
// getActivity() returns null if the activity is not attached
382+
when(mockHost.getActivity()).thenReturn(null);
383+
381384
// Create the real object that we're testing.
382385
FlutterActivityAndFragmentDelegate delegate = new FlutterActivityAndFragmentDelegate(mockHost);
383386

384387
// --- Execute the behavior under test ---
385388
// Flutter is attached to the surrounding Activity in onAttach.
386389
delegate.onAttach(RuntimeEnvironment.application);
387390

388-
// Verify that the ActivityControlSurface was NOT told to attach to an Activity.
389-
verify(mockFlutterEngine.getActivityControlSurface(), never())
390-
.attachToActivity(any(Activity.class), any(Lifecycle.class));
391+
// Make sure all of the other lifecycle methods can run safely as well
392+
// without a valid Activity
393+
delegate.onCreateView(null, null, null);
394+
delegate.onStart();
395+
delegate.onResume();
396+
delegate.onPause();
397+
delegate.onStop();
398+
delegate.onDestroyView();
391399

392400
// Flutter is detached from the surrounding Activity in onDetach.
393401
delegate.onDetach();
394402

395-
// Verify that the ActivityControlSurface was NOT told to detach from the Activity.
403+
// Verify that the ActivityControlSurface was NOT told to attach or detach to an Activity.
404+
verify(mockFlutterEngine.getActivityControlSurface(), never())
405+
.attachToActivity(any(Activity.class), any(Lifecycle.class));
396406
verify(mockFlutterEngine.getActivityControlSurface(), never()).detachFromActivity();
397407
}
398408

0 commit comments

Comments
 (0)