未验证 提交 d9d92a56 编写于 作者: M Matt Carroll 提交者: GitHub

Added FlutterActivity and FlutterFragment hook to cleanUpFlutterEngine() as...

Added FlutterActivity and FlutterFragment hook to cleanUpFlutterEngine() as symmetry for configureFlutterEngine(). (#41943) (#12987)
上级 6611e170
...@@ -851,6 +851,17 @@ public class FlutterActivity extends Activity ...@@ -851,6 +851,17 @@ public class FlutterActivity extends Activity
// No-op. Hook for subclasses. // 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 * Hook for subclasses to control whether or not the {@link FlutterFragment} within this
* {@code Activity} automatically attaches its {@link FlutterEngine} to this {@code Activity}. * {@code Activity} automatically attaches its {@link FlutterEngine} to this {@code Activity}.
......
...@@ -421,6 +421,10 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; ...@@ -421,6 +421,10 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
Log.v(TAG, "onDetach()"); Log.v(TAG, "onDetach()");
ensureAlive(); ensureAlive();
// Give the host an opportunity to cleanup any references that were created in
// configureFlutterEngine().
host.cleanUpFlutterEngine(flutterEngine);
if (host.shouldAttachEngineToActivity()) { if (host.shouldAttachEngineToActivity()) {
// Notify plugins that they are no longer attached to an Activity. // Notify plugins that they are no longer attached to an Activity.
Log.d(TAG, "Detaching FlutterEngine from the Activity that owns this Fragment."); 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; ...@@ -693,6 +697,12 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
*/ */
void configureFlutterEngine(@NonNull FlutterEngine flutterEngine); 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 * Returns true if the {@link FlutterEngine}'s plugin system should be connected to the
* host {@link Activity}, allowing plugins to interact with it. * host {@link Activity}, allowing plugins to interact with it.
......
...@@ -30,4 +30,10 @@ public interface FlutterEngineConfigurator { ...@@ -30,4 +30,10 @@ public interface FlutterEngineConfigurator {
* {@code Activity} at the time that this method is invoked. * {@code Activity} at the time that this method is invoked.
*/ */
void configureFlutterEngine(@NonNull FlutterEngine flutterEngine); 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);
} }
...@@ -924,6 +924,20 @@ public class FlutterFragment extends Fragment implements FlutterActivityAndFragm ...@@ -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 * See {@link NewEngineFragmentBuilder#shouldAttachEngineToActivity()} and
* {@link CachedEngineFragmentBuilder#shouldAttachEngineToActivity()}. * {@link CachedEngineFragmentBuilder#shouldAttachEngineToActivity()}.
......
...@@ -550,6 +550,17 @@ public class FlutterFragmentActivity extends FragmentActivity ...@@ -550,6 +550,17 @@ public class FlutterFragmentActivity extends FragmentActivity
// No-op. Hook for subclasses. // 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. * The path to the bundle that contains this Flutter app's resources, e.g., Dart code snapshots.
* <p> * <p>
......
...@@ -197,6 +197,21 @@ public class FlutterActivityAndFragmentDelegateTest { ...@@ -197,6 +197,21 @@ public class FlutterActivityAndFragmentDelegateTest {
verify(mockHost, times(1)).configureFlutterEngine(mockFlutterEngine); 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 @Test
public void itSendsInitialRouteToFlutter() { public void itSendsInitialRouteToFlutter() {
// ---- Test setup ---- // ---- Test setup ----
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册