fix(webview): preserve internal WebView state across rotation (#47)#51
fix(webview): preserve internal WebView state across rotation (#47)#51jim-daf wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes InternalWebViewActivity losing the user’s current in-WebView navigation state on device rotation by attempting to restore WebView state instead of always reloading the entry URL.
Changes:
- Restore
WebViewstate fromsavedInstanceStatewhen available. - Fall back to
loadUrl(startURL)when no saved state exists.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (savedInstanceState != null) { | ||
| // Issue #47: restore the saved WebView state on rotation | ||
| // instead of force-reloading the original URL. | ||
| binding.webview.restoreState(savedInstanceState); | ||
| } else { | ||
| binding.webview.loadUrl(startURL); | ||
| } |
There was a problem hiding this comment.
This code restores WebView state on rotation, but this class currently doesn’t override onSaveInstanceState() to call binding.webview.saveState(outState). Without saving the WebView snapshot, restoreState(savedInstanceState) will typically restore nothing. Add onSaveInstanceState() and save the WebView state into the bundle.
| if (savedInstanceState != null) { | ||
| // Issue #47: restore the saved WebView state on rotation | ||
| // instead of force-reloading the original URL. | ||
| binding.webview.restoreState(savedInstanceState); |
There was a problem hiding this comment.
WebView.restoreState(...) can return null when it can’t restore the state. In that case, this code skips loadUrl(startURL) and can leave a blank WebView. Capture the return value and fall back to loadUrl(startURL) when restoration fails.
| binding.webview.restoreState(savedInstanceState); | |
| if (binding.webview.restoreState(savedInstanceState) == null) { | |
| binding.webview.loadUrl(startURL); | |
| } |
| if (savedInstanceState != null) { | ||
| // Issue #47: restore the saved WebView state on rotation | ||
| // instead of force-reloading the original URL. | ||
| binding.webview.restoreState(savedInstanceState); | ||
| } else { |
There was a problem hiding this comment.
savedInstanceState is referenced inside configureWebview() but that method doesn’t have a Bundle parameter and there’s no field with that name, so this won’t compile. Pass the savedInstanceState from onCreate() into configureWebview(Bundle) (or store it in a field) and use that value here.
Closes #47.
Same root cause as #46. Rotating the device while inside
InternalWebViewActivityrebuilt the WebView and reloaded the entry URL, throwing away whatever the user had navigated to.This change adds
onSaveInstanceStateto persist the WebView and restores it on rotation, falling back to the originalloadUrlwhen there is no saved snapshot.