Skip to content

Commit

Permalink
Added FlutterActivity and FlutterFragment hook to cleanUpFlutterEngin…
Browse files Browse the repository at this point in the history
…e() as symmetry for configureFlutterEngine(). (flutter#41943) (flutter#12987)
  • Loading branch information
matthew-carroll authored Oct 11, 2019
1 parent 6611e17 commit d9d92a5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
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
* {@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

0 comments on commit d9d92a5

Please sign in to comment.