Added FlutterActivity and FlutterFragment hook to cleanUpFlutterEngine() as symmetry for configureFlutterEngine(). (#41943) (flutter/engine#12987)

This commit is contained in:
Matt Carroll
2019-10-11 13:52:21 -07:00
committed by GitHub
parent 20556a05cb
commit 5e17ccdf32
6 changed files with 67 additions and 0 deletions

View File

@@ -851,6 +851,17 @@ public class FlutterActivity extends Activity
// 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.
}
/**
* Hook for subclasses to control whether or not the {@link FlutterFragment} within this
* {@code Activity} automatically attaches its {@link FlutterEngine} to this {@code Activity}.

View File

@@ -421,6 +421,10 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
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.");
@@ -693,6 +697,12 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
*/
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.

View File

@@ -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);
}

View File

@@ -924,6 +924,20 @@ public class FlutterFragment extends Fragment implements FlutterActivityAndFragm
}
}
/**
* 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()}.

View File

@@ -550,6 +550,17 @@ public class FlutterFragmentActivity extends FragmentActivity
// 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>

View File

@@ -197,6 +197,21 @@ public class FlutterActivityAndFragmentDelegateTest {
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 ----