Skip to content

Keep the page across process death - #233

Open
dkodr wants to merge 2 commits into
cylonid:devfrom
dkodr:feat/webview-state
Open

dkodr wants to merge 2 commits into
cylonid:devfrom
dkodr:feat/webview-state

Conversation

@dkodr

@dkodr dkodr commented Sep 11, 2026

Copy link
Copy Markdown

WebViewActivity saves no state, so when Android reclaims the process the activity
comes back on the start URL with the scroll position, open view and form contents
gone. Cookies live on disk, so it was never a logout — but every reclaim throws the
user back to the top of the app.

Saving

Through WebViewCompat.saveState rather than the framework WebView.saveState.
The 1MB savedInstanceState budget is shared by the whole process, and web apps
without sandboxing all live in the main one, so several open web apps can exceed it
together and crash with TransactionTooLargeException. The Jetpack call takes a byte
budget and drops the oldest history entries to fit, without touching the live history,
so backward navigation stays intact. Forward entries are dropped as well, since there
is no forward button in the UI.

Only saves where the WebView reports SAVE_STATE. Falling back to the framework call
would reintroduce exactly the crash the budget exists to prevent, so older WebViews
keep today's behaviour instead — no restore, but no new failure mode either.

The second commit turns on the back-forward cache, so going back inside a web app
restores the previous page from memory instead of re-running the load.

androidx.webkit goes 1.13.0 → 1.17.0 for both. It needs minCompileSdk 33, which is
lower than the 34 required by 1.13.0, so this is independent of the targetSdk work
in #231 and applies to dev as it stands.

Testing

On a Pixel running Android 17 (API 37, WebView 151), killing the backgrounded process
with am kill to reproduce what the system does under memory pressure:

  • One web app scrolled deep into a page: comes back at the same position instead of
    at the top.
  • Three web apps open at once, all in the same process, all with history: every one
    restored, no TransactionTooLargeException, no crash in logcat. This is the case
    the byte budget exists for.
  • A web app with access restriction enabled: the prompt still appears and the content
    stays hidden until it is answered, so the biometric path is unaffected.

Not verified: that the back-forward cache actually shortens back navigation. Enabling
the flag follows the documented API, but I could not measure the difference without
instrumenting WebView's network activity, so treat that commit as untested rather than
proven.

One thing I noticed while testing and did not touch: setupWebView() runs
unconditionally after the biometric branch in onCreate, so a protected web app
issues its network request before the prompt is answered. The content is not shown —
the view is hidden — so this is not a visual leak, and it predates this change. Happy
to fix it separately if you want.

WebViewActivity saved no state at all, so when Android reclaimed the process the
activity came back on the start URL with the scroll position, open view and form
contents gone. Cookies survive on disk, so this was never a logout, just a reset.

Save through WebViewCompat, which drops the oldest history entries to fit a byte
budget instead of risking TransactionTooLargeException: the 1MB savedInstanceState
limit is shared by the whole process, and web apps that are not sandboxed all live
in the main one. Forward entries are dropped too, since there is no forward button.

Only saves where the WebView reports SAVE_STATE. Falling back to the framework
saveState would reintroduce exactly the crash the budget exists to avoid.
Going back re-ran the page load. With the back-forward cache the previous page is
restored from memory, which is what makes in-app back feel instant rather than
like a browser reload.
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.

1 participant