Skip to content

Commit a190365

Browse files
committed
[webview_flutter_lwe] Fix navigation delegate callbacks after delegate replacement
When the navigation delegate is replaced after the WebView is created, the new delegate's platform channel handler was never registered because it is only set up when the view is first created. As a result the new delegate's callbacks (onUrlChange, onPageFinished, onProgress, etc.) were still delivered to the previous delegate and silently dropped. Bind the new delegate's channel handler when the delegate is set after the view has been created, and guard the handler setup so a delegate is bound at most once. This makes onUrlChange work when a delegate is swapped, which the 'can receive url changes' integration test exercises.
1 parent 47a34f0 commit a190365

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

packages/webview_flutter_lwe/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 0.4.2
22

33
* Implement `onUrlChange` for the navigation delegate.
4+
* Fix navigation delegate callbacks being dropped when the navigation delegate
5+
is replaced after the WebView is created.
46
* Add 1 integration test case.
57

68
## 0.4.1

packages/webview_flutter_lwe/lib/src/lwe_webview_controller.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ class LweWebViewController extends PlatformWebViewController {
2525

2626
final LweWebView _webview;
2727
late LweNavigationDelegate _lweNavigationDelegate;
28+
int? _viewId;
2829

2930
/// Called when [TizenView] is created.
3031
void onCreate(int viewId) {
32+
_viewId = viewId;
3133
if (_webview.hasNavigationDelegate) {
3234
_lweNavigationDelegate.onCreate(viewId);
3335
}
@@ -142,6 +144,15 @@ class LweWebViewController extends PlatformWebViewController {
142144
) async {
143145
_lweNavigationDelegate = handler;
144146
_webview.hasNavigationDelegate = true;
147+
// If the view has already been created, the previous delegate's method
148+
// call handler is the one currently bound to the platform channel. Bind
149+
// the new delegate now so its callbacks (e.g. onUrlChange, onPageFinished)
150+
// are the ones invoked; otherwise events would keep going to the old
151+
// delegate and be dropped.
152+
final int? viewId = _viewId;
153+
if (viewId != null) {
154+
handler.onCreate(viewId);
155+
}
145156
}
146157

147158
@override
@@ -383,6 +394,7 @@ class LweNavigationDelegate extends PlatformNavigationDelegate {
383394
LweNavigationDelegate(super.params) : super.implementation();
384395

385396
late final MethodChannel _navigationDelegateChannel;
397+
bool _isChannelCreated = false;
386398
PageEventCallback? _onPageFinished;
387399
PageEventCallback? _onPageStarted;
388400
ProgressCallback? _onProgress;
@@ -392,6 +404,13 @@ class LweNavigationDelegate extends PlatformNavigationDelegate {
392404

393405
/// Called when [TizenView] is created.
394406
void onCreate(int viewId) {
407+
// Guard against being bound twice for the same delegate instance; the
408+
// channel handler below always reads the latest callbacks, so re-binding
409+
// is unnecessary.
410+
if (_isChannelCreated) {
411+
return;
412+
}
413+
_isChannelCreated = true;
395414
_navigationDelegateChannel = MethodChannel(
396415
kLweNavigationDelegateChannelName + viewId.toString(),
397416
);

0 commit comments

Comments
 (0)