Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit d9d92a5

Browse files
Added FlutterActivity and FlutterFragment hook to cleanUpFlutterEngine() as symmetry for configureFlutterEngine(). (#41943) (#12987)
1 parent 6611e17 commit d9d92a5

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,17 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
851851
// No-op. Hook for subclasses.
852852
}
853853

854+
/**
855+
* Hook for the host to cleanup references that were established in
856+
* {@link #configureFlutterEngine(FlutterEngine)} before the host is destroyed or detached.
857+
* <p>
858+
* This method is called in {@link #onDestroy()}.
859+
*/
860+
@Override
861+
public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine) {
862+
// No-op. Hook for subclasses.
863+
}
864+
854865
/**
855866
* Hook for subclasses to control whether or not the {@link FlutterFragment} within this
856867
* {@code Activity} automatically attaches its {@link FlutterEngine} to this {@code Activity}.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@ void onDetach() {
421421
Log.v(TAG, "onDetach()");
422422
ensureAlive();
423423

424+
// Give the host an opportunity to cleanup any references that were created in
425+
// configureFlutterEngine().
426+
host.cleanUpFlutterEngine(flutterEngine);
427+
424428
if (host.shouldAttachEngineToActivity()) {
425429
// Notify plugins that they are no longer attached to an Activity.
426430
Log.d(TAG, "Detaching FlutterEngine from the Activity that owns this Fragment.");
@@ -693,6 +697,12 @@ private void ensureAlive() {
693697
*/
694698
void configureFlutterEngine(@NonNull FlutterEngine flutterEngine);
695699

700+
/**
701+
* Hook for the host to cleanup references that were established in
702+
* {@link #configureFlutterEngine(FlutterEngine)} before the host is destroyed or detached.
703+
*/
704+
void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine);
705+
696706
/**
697707
* Returns true if the {@link FlutterEngine}'s plugin system should be connected to the
698708
* host {@link Activity}, allowing plugins to interact with it.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ public interface FlutterEngineConfigurator {
3030
* {@code Activity} at the time that this method is invoked.
3131
*/
3232
void configureFlutterEngine(@NonNull FlutterEngine flutterEngine);
33+
34+
/**
35+
* Cleans up references that were established in {@link #configureFlutterEngine(FlutterEngine)}
36+
* before the host is destroyed or detached.
37+
*/
38+
void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine);
3339
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,20 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
924924
}
925925
}
926926

927+
/**
928+
* Hook for the host to cleanup references that were established in
929+
* {@link #configureFlutterEngine(FlutterEngine)} before the host is destroyed or detached.
930+
* <p>
931+
* This method is called in {@link #onDetach()}.
932+
*/
933+
@Override
934+
public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine) {
935+
FragmentActivity attachedActivity = getActivity();
936+
if (attachedActivity instanceof FlutterEngineConfigurator) {
937+
((FlutterEngineConfigurator) attachedActivity).cleanUpFlutterEngine(flutterEngine);
938+
}
939+
}
940+
927941
/**
928942
* See {@link NewEngineFragmentBuilder#shouldAttachEngineToActivity()} and
929943
* {@link CachedEngineFragmentBuilder#shouldAttachEngineToActivity()}.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,17 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
550550
// No-op. Hook for subclasses.
551551
}
552552

553+
/**
554+
* Hook for the host to cleanup references that were established in
555+
* {@link #configureFlutterEngine(FlutterEngine)} before the host is destroyed or detached.
556+
* <p>
557+
* This method is called in {@link #onDestroy()}.
558+
*/
559+
@Override
560+
public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine) {
561+
// No-op. Hook for subclasses.
562+
}
563+
553564
/**
554565
* The path to the bundle that contains this Flutter app's resources, e.g., Dart code snapshots.
555566
* <p>

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,21 @@ public void itGivesHostAnOpportunityToConfigureFlutterEngine() {
197197
verify(mockHost, times(1)).configureFlutterEngine(mockFlutterEngine);
198198
}
199199

200+
@Test
201+
public void itGivesHostAnOpportunityToCleanUpFlutterEngine() {
202+
// ---- Test setup ----
203+
// Create the real object that we're testing.
204+
FlutterActivityAndFragmentDelegate delegate = new FlutterActivityAndFragmentDelegate(mockHost);
205+
206+
// --- Execute the behavior under test ---
207+
// The FlutterEngine is created in onAttach().
208+
delegate.onAttach(RuntimeEnvironment.application);
209+
delegate.onDetach();
210+
211+
// Verify that the host was asked to configure our FlutterEngine.
212+
verify(mockHost, times(1)).cleanUpFlutterEngine(mockFlutterEngine);
213+
}
214+
200215
@Test
201216
public void itSendsInitialRouteToFlutter() {
202217
// ---- Test setup ----

0 commit comments

Comments
 (0)