diff --git a/packages/webview_flutter/webview_flutter_android/CHANGELOG.md b/packages/webview_flutter/webview_flutter_android/CHANGELOG.md index b5502ecd2577..d45e73556256 100644 --- a/packages/webview_flutter/webview_flutter_android/CHANGELOG.md +++ b/packages/webview_flutter/webview_flutter_android/CHANGELOG.md @@ -1,3 +1,7 @@ +## 3.1.2 + +* Fixes bug where an `AndroidWebViewController` couldn't be reused with a new `WebViewWidget`. + ## 3.1.1 * Fixes bug where a `AndroidNavigationDelegate` was required to load a request. diff --git a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/JavaObjectHostApiImpl.java b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/JavaObjectHostApiImpl.java index 978e5232657d..9cbf65b4c613 100644 --- a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/JavaObjectHostApiImpl.java +++ b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/JavaObjectHostApiImpl.java @@ -27,6 +27,10 @@ public JavaObjectHostApiImpl(InstanceManager instanceManager) { @Override public void dispose(@NonNull Long identifier) { + final Object instance = instanceManager.getInstance(identifier); + if (instance instanceof WebViewHostApiImpl.WebViewPlatformView) { + ((WebViewHostApiImpl.WebViewPlatformView) instance).destroy(); + } instanceManager.remove(identifier); } } diff --git a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewHostApiImpl.java b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewHostApiImpl.java index 0a044cc5ab7e..77d535b78aed 100644 --- a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewHostApiImpl.java +++ b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewHostApiImpl.java @@ -102,9 +102,7 @@ public View getView() { } @Override - public void dispose() { - destroy(); - } + public void dispose() {} @Override public void setWebViewClient(WebViewClient webViewClient) { diff --git a/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewTest.java b/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewTest.java index c49d6c5d1142..1721ccdce8e4 100644 --- a/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewTest.java +++ b/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewTest.java @@ -281,4 +281,37 @@ public void defaultWebChromeClientDoesNotAttemptToCommunicateWithDart() { // This shouldn't throw an Exception. Objects.requireNonNull(webView.getWebChromeClient()).onProgressChanged(webView, 0); } + + @Test + public void disposeDoesNotCallDestroy() { + final boolean[] destroyCalled = {false}; + final WebViewPlatformView webView = + new WebViewPlatformView(mockContext, null, null) { + @Override + public void destroy() { + destroyCalled[0] = true; + } + }; + webView.dispose(); + + assertFalse(destroyCalled[0]); + } + + @Test + public void destroyWebViewWhenDisposedFromJavaObjectHostApi() { + final boolean[] destroyCalled = {false}; + final WebViewPlatformView webView = + new WebViewPlatformView(mockContext, null, null) { + @Override + public void destroy() { + destroyCalled[0] = true; + } + }; + + testInstanceManager.addDartCreatedInstance(webView, 0); + final JavaObjectHostApiImpl javaObjectHostApi = new JavaObjectHostApiImpl(testInstanceManager); + javaObjectHostApi.dispose(0L); + + assertTrue(destroyCalled[0]); + } } diff --git a/packages/webview_flutter/webview_flutter_android/example/integration_test/webview_flutter_test.dart b/packages/webview_flutter/webview_flutter_android/example/integration_test/webview_flutter_test.dart index dcd1dead700d..f6a55b1f2795 100644 --- a/packages/webview_flutter/webview_flutter_android/example/integration_test/webview_flutter_test.dart +++ b/packages/webview_flutter/webview_flutter_android/example/integration_test/webview_flutter_test.dart @@ -1018,6 +1018,51 @@ Future main() async { expect(elementText, 'null'); }, ); + + testWidgets( + '`AndroidWebViewController` can be reused with a new `AndroidWebViewWidget`', + (WidgetTester tester) async { + Completer pageLoaded = Completer(); + + final PlatformWebViewController controller = PlatformWebViewController( + const PlatformWebViewControllerCreationParams(), + ) + ..setPlatformNavigationDelegate(PlatformNavigationDelegate( + const PlatformNavigationDelegateCreationParams(), + )..setOnPageFinished((_) => pageLoaded.complete())) + ..loadRequest(LoadRequestParams(uri: Uri.parse(primaryUrl))); + + await tester.pumpWidget(Builder( + builder: (BuildContext context) { + return PlatformWebViewWidget( + PlatformWebViewWidgetCreationParams(controller: controller), + ).build(context); + }, + )); + + await pageLoaded.future; + + await tester.pumpWidget(Container()); + await tester.pumpAndSettle(); + + await tester.pumpWidget(Builder( + builder: (BuildContext context) { + return PlatformWebViewWidget( + PlatformWebViewWidgetCreationParams(controller: controller), + ).build(context); + }, + )); + + pageLoaded = Completer(); + await controller.loadRequest( + LoadRequestParams(uri: Uri.parse(primaryUrl)), + ); + await expectLater( + pageLoaded.future, + completes, + ); + }, + ); } /// Returns the value used for the HTTP User-Agent: request header in subsequent HTTP requests. diff --git a/packages/webview_flutter/webview_flutter_android/pubspec.yaml b/packages/webview_flutter/webview_flutter_android/pubspec.yaml index 72bc2e43414a..57346e147e7a 100644 --- a/packages/webview_flutter/webview_flutter_android/pubspec.yaml +++ b/packages/webview_flutter/webview_flutter_android/pubspec.yaml @@ -2,7 +2,7 @@ name: webview_flutter_android description: A Flutter plugin that provides a WebView widget on Android. repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutter/webview_flutter_android issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22 -version: 3.1.1 +version: 3.1.2 environment: sdk: ">=2.17.0 <3.0.0"