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 @@ -851,6 +851,17 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
// No-op. Hook for subclasses.
}

/**
* Hook for the host to cleanup references that were established in

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "clean up" here and below

* {@link #configureFlutterEngine(FlutterEngine)} before the host is destroyed or detached.
* <p>
* This method is called in {@link #onDestroy()}.
*/
@Override
public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine) {
// No-op. Hook for subclasses.
}

/**
* Hook for subclasses to control whether or not the {@link FlutterFragment} within this
* {@code Activity} automatically attaches its {@link FlutterEngine} to this {@code Activity}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ void onDetach() {
Log.v(TAG, "onDetach()");
ensureAlive();

// Give the host an opportunity to cleanup any references that were created in
// configureFlutterEngine().
host.cleanUpFlutterEngine(flutterEngine);

if (host.shouldAttachEngineToActivity()) {
// Notify plugins that they are no longer attached to an Activity.
Log.d(TAG, "Detaching FlutterEngine from the Activity that owns this Fragment.");
Expand Down Expand Up @@ -693,6 +697,12 @@ private void ensureAlive() {
*/
void configureFlutterEngine(@NonNull FlutterEngine flutterEngine);

/**
* Hook for the host to cleanup references that were established in
* {@link #configureFlutterEngine(FlutterEngine)} before the host is destroyed or detached.
*/
void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine);

/**
* Returns true if the {@link FlutterEngine}'s plugin system should be connected to the
* host {@link Activity}, allowing plugins to interact with it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ public interface FlutterEngineConfigurator {
* {@code Activity} at the time that this method is invoked.
*/
void configureFlutterEngine(@NonNull FlutterEngine flutterEngine);

/**
* Cleans up references that were established in {@link #configureFlutterEngine(FlutterEngine)}
* before the host is destroyed or detached.
*/
void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine);
}
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,20 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
}
}

/**
* Hook for the host to cleanup references that were established in
* {@link #configureFlutterEngine(FlutterEngine)} before the host is destroyed or detached.
* <p>
* This method is called in {@link #onDetach()}.
*/
@Override
public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine) {
FragmentActivity attachedActivity = getActivity();
if (attachedActivity instanceof FlutterEngineConfigurator) {
((FlutterEngineConfigurator) attachedActivity).cleanUpFlutterEngine(flutterEngine);
}
}

/**
* See {@link NewEngineFragmentBuilder#shouldAttachEngineToActivity()} and
* {@link CachedEngineFragmentBuilder#shouldAttachEngineToActivity()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,17 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
// No-op. Hook for subclasses.
}

/**
* Hook for the host to cleanup references that were established in
* {@link #configureFlutterEngine(FlutterEngine)} before the host is destroyed or detached.
* <p>
* This method is called in {@link #onDestroy()}.
*/
@Override
public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine) {
// No-op. Hook for subclasses.
}

/**
* The path to the bundle that contains this Flutter app's resources, e.g., Dart code snapshots.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,21 @@ public void itGivesHostAnOpportunityToConfigureFlutterEngine() {
verify(mockHost, times(1)).configureFlutterEngine(mockFlutterEngine);
}

@Test
public void itGivesHostAnOpportunityToCleanUpFlutterEngine() {
// ---- Test setup ----
// Create the real object that we're testing.
FlutterActivityAndFragmentDelegate delegate = new FlutterActivityAndFragmentDelegate(mockHost);

// --- Execute the behavior under test ---
// The FlutterEngine is created in onAttach().
delegate.onAttach(RuntimeEnvironment.application);
delegate.onDetach();

// Verify that the host was asked to configure our FlutterEngine.
verify(mockHost, times(1)).cleanUpFlutterEngine(mockFlutterEngine);
}

@Test
public void itSendsInitialRouteToFlutter() {
// ---- Test setup ----
Expand Down