Disconnect from platform views and virtual displays before detaching from the engine (flutter/engine#17379)

This commit is contained in:
Jason Simmons
2020-04-02 18:16:05 -07:00
committed by GitHub
parent ce0ec9042f
commit 3c089996dc
4 changed files with 41 additions and 3 deletions

View File

@@ -86,7 +86,7 @@ public class FlutterPluginRegistry
public void detach() { public void detach() {
mPlatformViewsController.detach(); mPlatformViewsController.detach();
mPlatformViewsController.onFlutterViewDestroyed(); mPlatformViewsController.onDetachedFromJNI();
mFlutterView = null; mFlutterView = null;
mActivity = null; mActivity = null;
} }
@@ -239,6 +239,6 @@ public class FlutterPluginRegistry
} }
public void destroy() { public void destroy() {
mPlatformViewsController.onFlutterViewDestroyed(); mPlatformViewsController.onDetachedFromJNI();
} }
} }

View File

@@ -225,6 +225,7 @@ public class FlutterEngine {
textInputChannel = new TextInputChannel(dartExecutor); textInputChannel = new TextInputChannel(dartExecutor);
this.platformViewsController = platformViewsController; this.platformViewsController = platformViewsController;
this.platformViewsController.onAttachedToJNI();
this.pluginRegistry = this.pluginRegistry =
new FlutterEnginePluginRegistry(context.getApplicationContext(), this, flutterLoader); new FlutterEnginePluginRegistry(context.getApplicationContext(), this, flutterLoader);
@@ -289,6 +290,7 @@ public class FlutterEngine {
Log.v(TAG, "Destroying."); Log.v(TAG, "Destroying.");
// The order that these things are destroyed is important. // The order that these things are destroyed is important.
pluginRegistry.destroy(); pluginRegistry.destroy();
platformViewsController.onDetachedFromJNI();
dartExecutor.onDetachedFromJNI(); dartExecutor.onDetachedFromJNI();
flutterJNI.removeEngineLifecycleListener(engineLifecycleListener); flutterJNI.removeEngineLifecycleListener(engineLifecycleListener);
flutterJNI.detachFromNativeAndReleaseResources(); flutterJNI.detachFromNativeAndReleaseResources();

View File

@@ -404,7 +404,21 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
return registry; return registry;
} }
public void onFlutterViewDestroyed() { /**
* Invoked when the {@link io.flutter.embedding.engine.FlutterEngine} that owns this {@link
* PlatformViewsController} attaches to JNI.
*/
public void onAttachedToJNI() {
// Currently no action needs to be taken after JNI attachment.
}
/**
* Invoked when the {@link io.flutter.embedding.engine.FlutterEngine} that owns this {@link
* PlatformViewsController} detaches from JNI.
*/
public void onDetachedFromJNI() {
// Dispose all virtual displays so that any future updates to textures will not be
// propagated to the native peer.
flushAllViews(); flushAllViews();
} }

View File

@@ -102,6 +102,28 @@ public class FlutterEngineTest {
verify(platformViewsController, times(1)).onPreEngineRestart(); verify(platformViewsController, times(1)).onPreEngineRestart();
} }
@Test
public void itNotifiesPlatformViewsControllerAboutJNILifecycle() {
FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
when(mockFlutterJNI.isAttached()).thenReturn(true);
PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
// Execute behavior under test.
FlutterEngine engine =
new FlutterEngine(
RuntimeEnvironment.application,
mock(FlutterLoader.class),
mockFlutterJNI,
platformViewsController,
/*dartVmArgs=*/ new String[] {},
/*automaticallyRegisterPlugins=*/ false);
verify(platformViewsController, times(1)).onAttachedToJNI();
engine.destroy();
verify(platformViewsController, times(1)).onDetachedFromJNI();
}
@Test @Test
public void itUsesApplicationContext() { public void itUsesApplicationContext() {
Context context = mock(Context.class); Context context = mock(Context.class);