Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,19 @@ public boolean shouldDispatchAppLifecycleState() {
return true;
}

/**
* Whether to automatically attach the {@link FlutterView} to the engine.
*
* <p>Returning {@code false} means that the task of attaching the {@link FlutterView} to the
* engine will be taken over by the host application.
*
* <p>Defaults to {@code true}.
*/
@Override
public boolean attachToEngineAutomatically() {
return true;
}

@Override
public boolean popSystemNavigator() {
// Hook for subclass. No-op if returns false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,10 @@ View onCreateView(
// Add listener to be notified when Flutter renders its first frame.
flutterView.addOnFirstFrameRenderedListener(flutterUiDisplayListener);

Log.v(TAG, "Attaching FlutterEngine to FlutterView.");
flutterView.attachToFlutterEngine(flutterEngine);
if (host.attachToEngineAutomatically()) {
Log.v(TAG, "Attaching FlutterEngine to FlutterView.");
flutterView.attachToFlutterEngine(flutterEngine);
}
flutterView.setId(flutterViewId);

if (shouldDelayFirstAndroidViewDraw) {
Expand Down Expand Up @@ -1171,5 +1173,17 @@ PlatformPlugin providePlatformPlugin(
* while return {@code true} means the engine dispatches these events.
*/
boolean shouldDispatchAppLifecycleState();

/**
* Whether to automatically attach the {@link FlutterView} to the engine.
*
* <p>In the add-to-app scenario where multiple {@link FlutterView} share the same {@link
* FlutterEngine}, the host application desires to determine the timing of attaching the {@link
* FlutterView} to the engine, for example, during the {@code onResume} instead of the {@code
* onCreateView}.
*
* <p>Defaults to {@code true}.
*/
boolean attachToEngineAutomatically();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,19 @@ public boolean shouldDispatchAppLifecycleState() {
return true;
}

/**
* Whether to automatically attach the {@link FlutterView} to the engine.
*
* <p>Returning {@code false} means that the task of attaching the {@link FlutterView} to the
* engine will be taken over by the host application.
*
* <p>Defaults to {@code true}.
*/
@Override
public boolean attachToEngineAutomatically() {
return true;
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static android.content.ComponentCallbacks2.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNotNull;
Expand Down Expand Up @@ -88,6 +90,7 @@ public void setup() {
when(mockHost.shouldHandleDeeplinking()).thenReturn(false);
when(mockHost.shouldDestroyEngineWithHost()).thenReturn(true);
when(mockHost.shouldDispatchAppLifecycleState()).thenReturn(true);
when(mockHost.attachToEngineAutomatically()).thenReturn(true);
}

@Test
Expand Down Expand Up @@ -1241,6 +1244,31 @@ public void usesFlutterEngineGroup() {
assertEquals(engineUnderTest, mockFlutterEngine);
}

@Test
public void itDoesAttachFlutterViewToEngine() {
// ---- Test setup ----
// Create the real object that we're testing.
FlutterActivityAndFragmentDelegate delegate = new FlutterActivityAndFragmentDelegate(mockHost);
delegate.onAttach(ctx);
delegate.onCreateView(null, null, null, 0, true);

// --- Execute the behavior under test ---
assertTrue(delegate.flutterView.isAttachedToFlutterEngine());
}

@Test
public void itDoesNotAttachFlutterViewToEngine() {
// ---- Test setup ----
// Create the real object that we're testing.
when(mockHost.attachToEngineAutomatically()).thenReturn(false);
FlutterActivityAndFragmentDelegate delegate = new FlutterActivityAndFragmentDelegate(mockHost);
delegate.onAttach(ctx);
delegate.onCreateView(null, null, null, 0, true);

// --- Execute the behavior under test ---
assertFalse(delegate.flutterView.isAttachedToFlutterEngine());
}

/**
* Creates a mock {@link io.flutter.embedding.engine.FlutterEngine}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ public boolean shouldDispatchAppLifecycleState() {
return true;
}

@Override
public boolean attachToEngineAutomatically() {
return true;
}

@Override
public void onFlutterSurfaceViewCreated(@NonNull FlutterSurfaceView flutterSurfaceView) {}

Expand Down