Skip to content

feat(webview): add release() helper that detaches and destroys cleanly (#562)#808

Open
jim-daf wants to merge 2 commits intoAwful:developfrom
jim-daf:feat/issue-562-webview-release-helper
Open

feat(webview): add release() helper that detaches and destroys cleanly (#562)#808
jim-daf wants to merge 2 commits intoAwful:developfrom
jim-daf:feat/issue-562-webview-release-helper

Conversation

@jim-daf
Copy link
Copy Markdown

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

Closes #562.

The StackOverflow answer linked in the issue boils down to: detach the WebView from its parent and call destroy() so Chromium can release its renderer process. Just letting the activity die leaks the surface and, on long sessions, eventually OOMs.

This PR adds an explicit release() helper on AwfulWebView that does the full cleanup dance (clear the JS interface so it does not retain the activity, navigate to about:blank, drop history, detach from parent, then destroy()). Hosting fragments and activities can call it from their own onDestroy / onDestroyView whenever they are sure they are done with the view.

public void release() {
    if (jsInterface != null) {
        removeJavascriptInterface(HANDLER_NAME_IN_JAVASCRIPT);
        jsInterface = null;
    }
    loadUrl("about:blank");
    clearHistory();
    ViewGroup parent = (ViewGroup) getParent();
    if (parent != null) {
        parent.removeView(this);
    }
    destroy();
}

Wiring the call sites is left out on purpose. The fragments that own a WebView each have their own teardown path, and rolling them all in one PR would balloon the diff.

@jim-daf jim-daf marked this pull request as ready for review April 25, 2026 19:27
Copilot AI review requested due to automatic review settings April 25, 2026 19:27
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

Adds an explicit teardown helper to AwfulWebView intended to prevent long-session WebView/Chromium renderer leaks by performing a more thorough cleanup sequence before destroy().

Changes:

  • Add AwfulWebView.release() helper to clear JS interface, navigate to about:blank, clear history, detach from parent, and destroy the WebView.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +134 to +136
ViewGroup parent = (ViewGroup) getParent();
if (parent != null) {
parent.removeView(this);
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.

Destroy the webview from time to time

2 participants