Skip to content

fix(webview): preserve internal WebView state across rotation (#47)#51

Open
jim-daf wants to merge 3 commits into
kidozh:masterfrom
jim-daf:fix/issue-47-internal-webview-rotation
Open

fix(webview): preserve internal WebView state across rotation (#47)#51
jim-daf wants to merge 3 commits into
kidozh:masterfrom
jim-daf:fix/issue-47-internal-webview-rotation

Conversation

@jim-daf
Copy link
Copy Markdown

@jim-daf jim-daf commented Apr 25, 2026

Closes #47.

Same root cause as #46. Rotating the device while inside InternalWebViewActivity rebuilt the WebView and reloaded the entry URL, throwing away whatever the user had navigated to.

This change adds onSaveInstanceState to persist the WebView and restores it on rotation, falling back to the original loadUrl when there is no saved snapshot.

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    if (binding != null) {
        binding.webview.saveState(outState);
    }
}
if (savedInstanceState != null) {
    binding.webview.restoreState(savedInstanceState);
} else {
    binding.webview.loadUrl(startURL);
}

@jim-daf jim-daf marked this pull request as ready for review April 25, 2026 20:48
Copilot AI review requested due to automatic review settings April 25, 2026 20:48
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 WebView state from savedInstanceState when 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.

Comment on lines +99 to +105
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);
}
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
if (savedInstanceState != null) {
// Issue #47: restore the saved WebView state on rotation
// instead of force-reloading the original URL.
binding.webview.restoreState(savedInstanceState);
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
binding.webview.restoreState(savedInstanceState);
if (binding.webview.restoreState(savedInstanceState) == null) {
binding.webview.loadUrl(startURL);
}

Copilot uses AI. Check for mistakes.
Comment on lines +99 to +103
if (savedInstanceState != null) {
// Issue #47: restore the saved WebView state on rotation
// instead of force-reloading the original URL.
binding.webview.restoreState(savedInstanceState);
} else {
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Internal WebView's content gets lost after rotation (similar to #46)

2 participants